Heaps and Priority Queues Questions
Binary heaps and priority queues for maintaining ordered access to the smallest or largest elements. Covers heapify, top-K selection, streaming medians via two-heap patterns, and merge-of-sorted-streams problems. Appears whenever a problem needs efficient repeated access to extremes without full sorting.
In Python, which heap implementation in the standard library is a min-heap, and how would you implement a max-heap using it? Provide a brief code snippet to show pushing and popping a max-heap using heapq.
You must support computing median of a stream with strict memory budget M where M < N (N = total elements seen). Propose algorithms to approximate the median under memory constraints and discuss error bounds and practical implementations (e.g., reservoir sampling, t-digest, GK-algorithm).
You have two heaps: lowers (max-heap) contains [5, 3, 2], highers (min-heap) contains [6, 8, 9]. Show the steps and resulting heaps after inserting value 7, including any rebalancing required. Explain your decision at each step.
Provide an algorithm to merge two running-median data structures (each represented by two heaps) into a single running-median structure that represents the combined streams. Focus on correctness and runtime complexity.
Behavioral: Describe a time when you had to debug a subtle data-structure invariant (not necessarily heaps) in production. Use STAR format: Situation, Task, Action, Result. Highlight how you communicated with stakeholders and prevented regressions.
Unlock Full Question Bank
Get access to all 36 Heaps and Priority Queues interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.