The K-th Nearest Obstacle Queries interview question is a dynamic distance-tracking task. You are at the origin and a stream of obstacles is added one by one at various coordinates. For each new obstacle, you need to find the smallest Manhattan distance among all obstacles seen so far. If fewer than obstacles exist, return -1.
Google ask this Obstacle Queries coding problem to evaluate your ability to maintain a sorted subset of a data stream. It tests your knowledge of Heaps (Priority Queues). The goal is to efficiently find the smallest value without sorting the entire history of obstacles ( per query).
This problem follows the Max-Heap for Top-K Smallest pattern.
, Obstacles at: [1, 2], [3, 4], [0, 1]
[3]. Return -1 (size < 2).[7, 3]. Top is 7. Return 7.[7, 3, 1]. Pop 7. Heap: [3, 1]. Top is 3. Return 3.
Result: [-1, 7, 3].Master the "Top-K" pattern. To find the smallest things, use a Max-Heap. To find the largest things, use a Min-Heap. This is a foundational Heap interview pattern for data stream processing.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Process Tasks Using Servers | Medium | Solve | |
| Find K Pairs with Smallest Sums | Medium | Solve | |
| Last Stone Weight | Easy | Solve | |
| Construct Target Array With Multiple Sums | Hard | Solve | |
| Campus Bikes | Medium | Solve |