Find the K Closest Points to a Query Location

Last updated: August 16, 2026

Quick Overview

The question asks you to find the K closest points to a given query location from a set of points in a 2D space.

Uber
Coding & Algorithms
Software Engineer
Uber
August 16, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

3

0

29 solved


The question asks you to find the K closest points to a given query location from a set of points in a 2D space.

This question is typically asked during the onsite interview round for software engineering roles at Uber. The interviewer is evaluating the candidate's problem-solving skills, ability to optimize algorithms, and understanding of data structures. Given the fast-paced nature of the interview, candidates are expected to quickly devise a solution and then discuss potential optimizations and edge cases.

What the Interviewer Expects
  • Clearly explain the approach to find the K closest points.
  • Use appropriate data structures to optimize the solution.
  • Discuss the time and space complexity of the chosen algorithm.
  • Handle edge cases and discuss potential optimizations.
  • Provide a working code implementation in Python.
Key Topics to Cover
Distance Calculation
Data Structures
Algorithm Optimization
Complexity Analysis
Edge Cases
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you modify your solution for a dynamic set of points?
  • What if the points were in a higher-dimensional space?
  • Can you explain how your approach scales with larger datasets?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Approach

To find the K closest points to a query location, we can use a max-heap to keep track of the closest points as we iterate through the list of points. The distance can be calculated using the Euclidean...

Algorithm
  1. Initialize a max-heap to store the K closest points.
  2. Iterate through each point in the list:
    • Calculate the squared distance from the query point.
    • If the heap has fewer than K points, a...

Submit Your Answer
Markdown supported

Related Questions