K-th Nearest Obstacle Queries - Problem
Imagine you're a city planner working on an infinite 2D grid where new buildings (obstacles) are being constructed one by one. After each new building is placed, you need to quickly determine the k-th closest building to the city center (origin) using Manhattan distance.

You'll receive a series of queries where each query [x, y] represents placing a new obstacle at coordinate (x, y). After each placement, calculate the Manhattan distance |x| + |y| from the origin to the k-th nearest obstacle.

Goal: Return an array where each element represents the distance to the k-th nearest obstacle after each query, or -1 if fewer than k obstacles exist.

Example: If k=2 and you place obstacles at (1,1), (2,2), (3,3), the results would be [-1, 4, 4] because initially there's only 1 obstacle, then the 2nd nearest has distance 4, and it remains 4 after the third placement.

Input & Output

example_1.py โ€” Basic Case
$ Input: k = 3, queries = [[1,2],[3,4],[2,3],[-3,0]]
โ€บ Output: [-1, -1, 7, 5]
๐Ÿ’ก Note: Query 1: distance = |1|+|2| = 3, only 1 obstacle, k=3 โ†’ -1. Query 2: distances = [3,7], only 2 obstacles, k=3 โ†’ -1. Query 3: distances = [3,5,7], 3rd nearest = 7. Query 4: distances = [3,3,5,7], 3rd nearest = 5.
example_2.py โ€” Edge Case k=1
$ Input: k = 1, queries = [[5,5],[4,4],[3,3]]
โ€บ Output: [10, 8, 6]
๐Ÿ’ก Note: With k=1, we always want the nearest obstacle. Query 1: nearest = 10. Query 2: distances = [8,10], nearest = 8. Query 3: distances = [6,8,10], nearest = 6.
example_3.py โ€” Same Distance
$ Input: k = 2, queries = [[1,1],[2,0],[0,2]]
โ€บ Output: [-1, 2, 2]
๐Ÿ’ก Note: Query 1: distance = 2, only 1 obstacle โ†’ -1. Query 2: distances = [2,2], 2nd nearest = 2. Query 3: distances = [2,2,2], all equal, 2nd nearest = 2.

Constraints

  • 1 โ‰ค k โ‰ค 105
  • 1 โ‰ค queries.length โ‰ค 2 ร— 104
  • All queries[i] are unique
  • -109 โ‰ค queries[i][0], queries[i][1] โ‰ค 109
  • No obstacle exists at a coordinate when it's being placed

Visualization

Tap to expand
Emergency Response Station PlanningCity CenterStation A (2,3)dist=5Station B (4,1)dist=5Station C (1,6)dist=7Max-Heap (k=2)7552nd nearest: 7Resource AllocationBased on k=2nd nearest station:Distance = 7 unitsResponse time: 14 minutesCoverage radius: 7 blocks
Understanding the Visualization
1
Station Placement
A new emergency station is built at coordinates (x,y)
2
Distance Calculation
Calculate Manhattan distance |x| + |y| from city center
3
Heap Evaluation
Check if this station should be in our 'top k closest' list
4
Update Response Plan
Report the k-th closest station distance for resource planning
Key Takeaway
๐ŸŽฏ Key Insight: Using a max-heap of size k allows us to efficiently track only the k nearest distances without sorting all obstacles after each placement, reducing complexity from O(nยฒ log n) to O(n log k).
Asked in
Google 42 Amazon 38 Apple 25 Microsoft 31
73.4K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen