Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Closest Stars

Learn
Play
Prove
Problem context and objectives
Mission briefing

Matchmaking coordinator · Urgent players and jobs compete for limited slots

Keep the best next candidate available without sorting everything again: The Closest Stars.

A stale priority frontier increases wait time for everyone.

How you win

  1. 1Recognize when Bounded max-heap selection matches the clues
  2. 2Keep this true after every move: the heap contains the k closest processed points
  3. 3Reach the result within O(n log k)

Rules and pressure

  • Target cost: O(n log k)
  • State rule: the heap contains the k closest processed points
Lesson 1 of 3

Live algorithm trace

Bounded max-heap selection

Complete execution
1 of 6
1,3
point
-2,2
5,8
0,1

Create a max-heap ordered by squared distance.

1heap = max heap by squared distance
2push each point
3if heap size exceeds k: pop farthest
4sort the survivors deterministically
5return survivors
k = 2heap = []
Truth to preserve / Cost target
Truth to preserve

the heap contains the k closest processed points

Cost target

O(n log k)

Maintain a max-heap of k chosen points by squared distance. The root is the farthest current winner, so any closer point replaces it without storing or sorting every point.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead