Dsa
Binary Search Tree
Medium
Proximity Alert System
You are building a proximity alert system for a naval radar. As vessels are detected, their distance from the shore (as an integer) is recorded in a Binary Search Tree to maintain sorted order efficiently. For every new vessel detected, you must report the distance to its nearest neighbor already in the system. If it is the first vessel, report -1.
Input: A single line of space-separated integers representing the order of vessel detections.
Output: A single line of space-separated integers representing the proximity to the closest existing vessel for each detection.
Example:
Input: 50 30 80 35
Output: -1 20 30 5
Explanation:
- 50: First node, -1.
- 30: Closest to 50 is 50. Dist: 20.
- 80: Closest to 50 is 50. Dist: 30.
- 35: Closest to 30 is 30. Dist: 5. (Closer than 50)
Key concepts
binary_search_treenearest_neighborordered_set
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now