Heaps interview problems
Heap questions in a practical round look like systems work. Keep the top k items from a stream, merge sorted log files, run the next scheduled job, or expire the oldest entry. Python's heapq is a min-heap over a plain list with heappush, heappop, heapreplace, heappushpop, nlargest, nsmallest and merge. There is no max-heap, so you negate numeric keys, and there is no decrease-key, so you push a new entry and skip stale ones when they come out.
The classic bug is pushing tuples like (priority, item) where two priorities tie and Python then compares the items, which raises TypeError for dicts or custom objects. The fix is a monotonic counter as the second element, (priority, seq, item), which also makes ties first-in first-out. Other mistakes include sorting the whole input when a bounded heap of size k gives O(n log k), and calling heapify on every insert. A strong answer explains why the heap stays size k, handles cancellation with lazy deletion, and states the complexity of each operation.
3 locked
These open in order of demand. Open one and press I want this one to ask for it.
- 05Priority Batch Scheduler Lockedmedium
Pick which inference requests go into one GPU batch: highest priority first, FIFO on ties, skip what does not fit, never exceed the token budget.
- 08Retry Scheduler with Exponential Backoff Lockedhard
Build a request scheduler that retries failures with exponential backoff, orders work by ready time and tracks a five-state lifecycle.
- 26Async Priority Job Scheduler Lockedhard
Run submitted async jobs on N workers in priority order, FIFO within a priority, with cancellation and a draining shutdown.