DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Heap

K Closest Points to Origin

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **K Closest Points to Origin** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Max-heap of size k, eject farthest **Edge Cases to Consider:** - (1) k = 1 - (2) k = all points - (3) Points equidistant

Examples

Example 1
Input: [[1,3],[-2,2]], 1
Output: [[-2,2]]
Explanation: dist²: 10 vs 8
Example 2
Input: [[3,3],[5,-1],[-2,4]], 2
Output: [[3,3],[-2,4]]
Explanation: dist²: 18, 26, 20
Example 3
Input: [[1,0]], 1
Output: [[1,0]]
Explanation: Single point

Constraints

  • ▪Input arguments are within valid ranges
  • ▪Optimize for execution speed
  • ▪Handle null/empty inputs gracefully

Watch Out For Edge Cases

  • ▪k = 1
  • ▪k = all points
  • ▪Points equidistant
Frequently Asked At
AmazonGoogleMicrosoftMetaAdobe