Heaps and Priority Queues Questions

Binary heaps and priority queues as data structures: min-heap and max-heap, array layout, sift-up and sift-down, push/pop/peek costs, build-heap (heapify) in O(n), and indexed heaps with decrease-key versus lazy deletion of stale entries. Covers the problems where a heap is the intended tool: top-K and kth largest or smallest selection (including over a one-pass stream and under inserts and deletes), sorting an array whose elements are at most k places out of order with a window heap, the two-heap pattern for running and sliding-window medians (invariants, rebalancing, lazy deletion, testing, and extensions to percentiles, weighted medians, custom orderings and merging structures), k-way merge of sorted lists, arrays, files or streams with bounded memory, scheduling by priority with cancellation and reprioritisation, stable priority queues with tie-breaking, and implementing a heap or priority queue with a language library such as heapq or PriorityQueue (including max-heap behaviour from a min-heap). Boundary: graph shortest-path algorithms, approximate quantile sketches, production streaming-system design, and heapsort and quickselect as sorting and selection algorithms are covered elsewhere.

MediumTechnical
78 practiced

Implement a RunningMedian class in Python with add(num) and get_median(). Each add should be O(log n) and get_median O(1). Handle even and odd counts, duplicates, and a call to get_median on an empty stream, and state the complexity of your solution.

EasyTechnical
74 practiced

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.

MediumTechnical
81 practiced

An array of n integers has the property that every element is at most k positions away from where it would be in sorted order. Sort it efficiently. Which approach do you choose, how does a heap of size k+1 make it work, and what are the time and space complexities compared with a general sort when k is small?

MediumTechnical
61 practiced

A binary heap supports efficient removal of only its top element. How would you support deleting an arbitrary value from a structure built on heaps, such as a two-heap median? Compare lazy deletion with true removal from the array and with an indexed heap, including the complexity and memory trade-offs.

MediumTechnical
79 practiced

Implement a function in JavaScript to find the kth most frequent element in an array of integers. Provide a solution optimized for average-case time complexity better than O(n log n) where n is array length. State assumptions and complexity.

Unlock Full Question Bank

Get access to all 28 Heaps and Priority Queues interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.