easyHeap / Priority QueueAmazonFacebookApple
K-Floor VIP Keeper
A hotel keeps only the K highest-tier guests on its exclusive floor; each new arrival may bump out the lowest-tier VIP.
Design a class that tracks the k-th largest element in a stream. Each `add(val)` returns the current k-th largest.
Choose approach
4
newest
1/24
Narrationstep 1 of 24
Seed value 4. Sift up to maintain min-heap. Current heap size = 1.
Variables
added=4
size=1
k=3
How to think about it
- Only the top K matter. The smallest among those IS the k-th largest.
- Min-heap's root is the smallest -> O(1) answer.
- On overflow, pop the root. O(log k) per operation.
Edge cases to catch in interview
- Initial list smaller than k -> heap grows until first k are seen.
- Repeated values -> treated as multiset.
Examples
Example 1
[4,5,5,8,8]{"k":3,"initial":[4,5,8,2],"stream":[3,5,10,9,4]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed