FDEInterviews logoFDE/Interviews
Coding & DSA / 85
mediumMetaAmazon

Find the k closest points to the origin

The tell is whether you avoid the trap of sorting all n points when you only need k. Two real answers: a max-heap of size k at O(n log k), or quickselect at O(n) average. Knowing which one the interviewer wants is the actual question.

Updated Aug 2026 · Grounded in real Forward Deployed Engineer interview loops and written to a senior-engineer editorial bar.

The tell is whether you avoid the trap of sorting all n points when you only need k. Two real answers: a max-heap of size k at O(n log k), or quickselect at O(n) average. Knowing which one the interviewer wants is the actual question.

Unlock the other 466 answers · ₹2,000 / $25Your progress and mastery stay saved · 6 months · one payment · no auto-renew
UP NEXT ON YOUR JOURNEY
FEDITOR'S NOTE

The reserved follow-up is 'do we need to sort the full list?', and the answer is no: you need the k closest in any order, not a full ranking, so a total sort at O(n log n) is wasted work. Leading with the size-k heap and then offering quickselect as the O(n)-average alternative, with the conditions under which each wins, is the staff-level signal. Comparing squared distances instead of taking square roots is the small detail that shows you've actually thought about it.

DISCUSSION · 0

No comments yet — be the first to share your approach.