Daily iOS
CS fundamentals and algorithms

K nearest places

You have tens of thousands of places with an id and coordinates (latitude, longitude). Given the user's current location, design a function that finds the 10 nearest places and returns their center coordinate. Which algorithm and data structure do you use, and what's the complexity?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Compute the distance for all n while maintaining a max heap of size 10, O(n log k) time and O(k) space, or use quickselect for O(n) on a one-off query. For distance, don't use raw latitude and longitude as Euclidean; use haversine or a latitude-corrected approximation.

Share a question