Study
When Is a Heap the Right Answer in a Coding Interview?
TrueInterview
October 18, 2026 · 14 min read

Use a heap when the input arrives as a stream or when you repeatedly need the current best item; use buckets when the ranking key is a bounded integer such as a frequency, sort when every element must come back in order, and offer quickselect only when asked to beat O(n log k). As of 2026-10-18, TrueInterview's bank holds 76 candidate-reported heap and top-K questions from 22 big-tech companies, and 49 of them were reported in phone screens. The most-linked one is GPU Credits II, linked from 14 candidate write-ups, ahead of textbook Top-K. Our view: the heap matters less as a Top-K trick than as the engine of a simulation.
Disclosure: TrueInterview is an interview-preparation product and publishes this article. Facts about other products come from their public pages on the dates listed under Sources.
How do you choose between a heap, sorting, quickselect and buckets?
Ask three questions before you write code. Is the input a stream, or do you need the current best item again and again? Is the ranking key a bounded integer, such as a count, or an arbitrary distance or score? Must the output be sorted? The answers pick the method, and the interview signal is saying why the other methods fit worse.
PracHub's Top K Frequent Elements guide puts the signal plainly: it is "knowing which one to reach for under the constraints in front of you, and saying why the others fit worse". The table below collects the trade-offs from the public sources, each named in its row.
| Criterion | Full sort | Heapify, then pop k | Size-k heap | Quickselect | Buckets by count |
|---|---|---|---|---|---|
| Time | O(n log n), then take the first k (Nil Mamano's blog) | O(n + k log n) (Nil Mamano's blog) | O(n log k) (Hello Interview) | Linear on average, quadratic worst case (Wikipedia) | O(n) when counts are bounded by n (Hello Interview) |
| Extra space | Built-in sorts use O(n) internally (Nil Mamano's blog) | O(n) overall: heapify is in place on a copy of the input (Python docs, Nil Mamano's blog) | O(k) beyond the counts (PracHub) | In place (Wikipedia) | One bucket per possible count |
| Works on a stream | No | No | Yes (PracHub) | No | Only as a frequency-bucket structure with amortized O(1) updates (PracHub) |
| Key it needs | Any comparable key | Any comparable key | Any comparable key | Any comparable key | A bounded integer only (PracHub) |
| Best for | Problems that need every element in order | Static in-memory input when O(n) extra space is fine | The default answer, and any distance or score key | A follow-up asking you to beat O(n log k) on static data | Frequency Top-K when k can be large |
Our take: lead with the size-k heap in almost every Top-K prompt, because it is the one method that survives every constraint change the interviewer can make. Then name buckets if the key is a count, and name quickselect only if you are asked to go faster. Sorting is the right answer more often than candidates think, but only when the problem asks for the whole ordering.
Which heap questions do candidates report most?
By our reading of the titles, the heap questions most linked from candidate write-ups lean toward scheduling and resource problems rather than plain Top-K selection. GPU Credits II leads with 14 write-ups, Meeting Rooms / Interval Scheduling Variants has 9, and Closest Cake and Global Assignment and Employee Referral Program have 8 each. Top K Frequent Elements also has 8, spread across 6 companies.

The table ranks the questions by write-up links and adds the split by candidates' self-reported level. The "shape" column is our reading of each title, not a description from the reports.
| Question | Companies in the write-ups | Write-up links | Senior-or-above vs junior-or-below | Shape (our read) | Practise |
|---|---|---|---|---|---|
| GPU Credits II | OpenAI | 14 | 11 vs 3 | Time-ordered resource accounting | GPU Credits II |
| Meeting Rooms / Interval Scheduling Variants | 9 | 6 vs 3 | Intervals ordered by start or end time | Meeting Rooms variants | |
| Closest Cake and Global Assignment | Snowflake | 8 | Not split | Assignment by distance | Closest Cake |
| Employee Referral Program | Robinhood | 8 | 4 vs 4 | Ranked leaderboard | Employee Referral Program |
| Top K Frequent Elements | Amazon, LinkedIn, Microsoft, Uber, Google, Apple | 8 | 4 vs 4 | Count, then select | Top K Frequent Elements |
| Meeting Rooms III | Amazon, ByteDance, Uber | 6 | 3 vs 3 | Heap of room end times | Meeting Rooms III |
| Top K Largest Elements | Amazon, Uber, LinkedIn, Microsoft | 6 | 2 senior-or-above | Size-k min-heap | Top K Largest Elements |
| Job Scheduler | Lyft | 5 | 3 vs 2 | Heap of finish times | Job Scheduler |
Write-up links count the candidate write-ups that point to a question; they do not measure how often a company asks it. Our take: if you only prepare textbook Top-K, you have prepared for one row of this table. Six of the eight rows have no k in the title at all, and several of them, by our read, are time-ordered simulations with a priority queue at the centre.
Two classic heap shapes belong next to the table. Find Median from Data Stream is filed under 6 big-tech companies and needs two balanced heaps; the Python documentation says a running median "can be efficiently implemented by balancing two heaps". Merge K Sorted Lists is filed under 4 companies but linked from only 1 write-up, and the Python documentation describes heapq.merge as merging sorted inputs without pulling all the data into memory at once. Learn both templates, but give the simulation problems the larger share of your time.
Which rounds report heap questions, and what does that mean for your prep?
Heap questions are mostly phone-screen material. Of the 76 questions, 49 were reported in phone screens, 29 onsite and 8 in online assessments, and a question can appear in more than one round. Our view: a slow heap in a phone screen can end the loop before the onsite, so the size-k heap has to come out of your hands in a few minutes.
The co-tags shift a little by round. Phone-screen heap questions most often add hashing (9) and greedy (7), while onsite ones add greedy (7) and hashing (5). Greedy is a larger share of the onsite set than of the phone-screen set, so keep greedy-plus-heap problems for the weeks before an onsite.
PracHub's guide says that at companies calibrating on LeetCode 347 the heap is "the baseline competent answer". The same guide reports one Amazon candidate who solved the problem by brute force, was told to rewrite it with a heap, and ran out of time mid-rewrite. One report is not a pattern, but it shows the cost of treating the heap as an optimisation you add later.
By company, Amazon has the most heap questions in the bank (17), followed by Google (14), Uber (11), ByteDance (10) and Meta (9). Dated examples show the range of what candidates reported:
| Company | Question | Round | Last reported |
|---|---|---|---|
| OpenAI | GPU Credits II | Phone screen | 2026-09 |
| Meeting Rooms / Interval Scheduling Variants | Phone screen | 2026-06 | |
| Snowflake | Closest Cake and Global Assignment | Phone screen | 2026-06 |
| Robinhood | Employee Referral Program | Phone screen | 2026-04 |
| Microsoft | Top K Largest Elements | Phone screen | 2026-08 |
| Lyft | Job Scheduler | Onsite | 2026-07 |
These are candidate-reported questions reconstructed for practice, not any company's official question list. The set is also current: 67 of the 76 were last reported within the 12 months before 2026-10-18.
How does one problem change answer when one constraint changes?
Take one problem family and change one constraint at a time. The counting pass stays the same, and the selection step moves between buckets, a heap, a full ordering and a simulation. PracHub's guide says the counting pass "is identical in every solution" and that interviews are decided by the selection step after it.
Top K Frequent Elements: buckets or a size-k heap
Top K Frequent Elements was reported in an Apple phone screen in 2026-09 and is filed under 7 big-tech companies. Hello Interview's problem page says it is typically solved with a frequency map plus a min-heap at O(n log k) or bucket sort at O(n). The same page states that the time complexity "must be better than O(n log n)", so sorting the whole frequency map misses the bound. Buckets work here because, as PracHub's guide says, a frequency is an integer between 1 and n.
K Closest Points to Origin: the key is a distance
K Closest Points to Origin (N log K) was reported in a Snapchat phone screen in 2026-08. PracHub's guide says that "the moment the ranking key is a distance or a score, buckets stop applying". The size-k heap becomes the answer, which is why it is the safer default to lead with.
Sort Elements by Frequency: the output must be ordered
Sort Elements by Frequency was reported in an Amazon phone screen in 2026-08. Every element comes back in order, which rules out a size-k heap and quickselect. A comparison sort gives the order in O(n log n), and buckets by count give it in O(n) because the key is still a frequency; if ties need a specific order, the sort with a two-part key is the simpler code to defend.
Meeting Rooms III: no k at all
Meeting Rooms III was reported in a ByteDance phone screen in 2026-09. In the familiar LeetCode version, a min-heap of room end times tells you which room frees up first, and a second heap of free room ids gives the lowest-numbered free room. By our reading of the titles, several of the heavily linked questions share this shape: the heap drives a time-ordered simulation, and the hard part is deciding what enters and leaves it.
Is quickselect worth leading with?
Mention quickselect, but do not lead with it in a timed screen. Wikipedia's quickselect article says its average performance is good but its worst case is poor. The code also has more edge cases than a size-k heap. Nil Mamano's blog calls quickselect "not common in interviews", which makes it a follow-up answer, not an opening one.
Wikipedia's article on quickselect explains that it recurses into only the side of the partition that holds the target. The same article says that "the easiest solution is to choose a random pivot, which yields almost certain linear time". Median of medians guarantees a linear worst case, but the Wikipedia article notes it is generally not used in practice because the pivot costs too much.
PracHub's guide reports one Google phone screen in which the interviewer asked whether the solution could beat O(n log n) and how to reduce its space footprint. Those two follow-ups map onto a known trade, and Nil Mamano's blog sums it up: quickselect is best for runtime and heaps are best for space. The same blog says sorted output raises quickselect to O(n + k log k), and that duplicates of the kth value need a loop so the result has exactly k elements. PracHub's guide advises naming the O(n²) worst case and the random-pivot fix unprompted if you propose it.
Our take: quickselect is a good answer to a follow-up about beating O(n log k) and a poor answer to the opening prompt. Write the heap, state its complexity, then offer quickselect with its worst case in one sentence. That order gets you a working solution and the extra credit.
What changes when the input never ends?
On a stream, the heap still selects candidates, but it does not hold the counts. PracHub's streaming guide says a heap "does not replace the frequency state needed to know which items deserve selection". You need a hash map of counts next to the heap, and the interviewer will ask how big that map can get.
PracHub's streaming guide states that keeping only a heap of k keys is not exact. If every distinct key fits in memory, the same guide recommends an exact hash map from key to count; if not, it says to state an approximation contract. For sliding windows, where old events expire, the guide's warning is direct: "Do not silently reuse an insertion-only proof".
Our take: in a coding round, name the approximation contract in one or two sentences and move on. Count-Min Sketch and Misra-Gries belong to system design rounds; going deep on them in a coding screen spends time the interviewer wanted for your code.
Where do strong heap answers lose points?
Competent candidates rarely lose points on the heap itself. They lose them on the comparator, ties and the frequency state around it. Hello Interview's problem page lists a max-heap used where a min-heap is needed as a common mistake, because it makes the lowest-frequency item hard to evict when the heap is full.
Hello Interview's page lists two more mistakes: a heap that grows to size n instead of k, and returning the heap structure itself instead of the extracted items. The same page notes that ties can come back in any order, so a solution should not assume a specific tie ordering. PracHub's guide adds that tie-breaking rules test whether you can build a comparator and show up as warm-ups at ByteDance and Squarepoint.
Python has its own traps. The Python documentation says heap elements can be tuples, which lets you attach a priority to a record. PracHub's guide warns that tuple comparison on a tie works for integers but throws for objects with no ordering, so push a counter between the priority and the item. The Python documentation also says heapreplace suits a fixed-size heap, and that a heapsort built with heapq is not stable, unlike sorted().
Our take: before writing the comparator, ask the interviewer how ties should be ordered. It costs ten seconds and removes the most common reason a correct heap answer still gets a follow-up you did not want.
What changes at senior and staff level?
Write-up links to these questions come 71 times from candidates who reported a senior, staff-plus or manager level and 60 times from junior, new-grad or intern candidates. Without the bank-wide level split as a baseline, that gap may reflect who writes up interviews rather than anything about heap questions. The clearest single case is GPU Credits II, where senior-or-above write-ups dominate.
By algorithm topic, intervals-tagged links split 9 senior-or-above to 3 junior-or-below, and sorting-tagged links 7 to 3, while graphs-tagged links lean junior at 9 to 14. Treat the intervals figure as a lean, not a rule: only 2 of the 76 questions carry the intervals tag. Shard Rebalancing, linked from 3 senior-or-above write-ups, points the same way: the heap sits inside a resource model.
Our take: at senior level, expect the heap to arrive inside a scheduling or resource problem, and lead with the constraint discussion before you are asked. Say whether the input is a stream, how much memory the counts can take, whether the answer must be exact and how ties break. That judgement is what separates a baseline heap answer from a senior one.
Down-leveling note (our view): a clean heap solution in a phone screen rarely sets your level on its own. In our view, the level signal comes mostly from the design depth, the project deep dive and the scope in your behavioral stories. A slow or buggy heap, though, can stop the loop before those rounds happen, which is why fluency here protects the rounds that do carry the level.
How we counted
Counts come from TrueInterview's question bank as of 2026-10-18 and cover 76 heap and top-K practice questions that candidates reported at 22 big-tech companies. A question can be filed under several companies and reported in several rounds, so tallies overlap. Level splits count write-up links by the level each candidate reported, and a link count measures what candidates wrote up, not how often a company asks a question.
FAQ
Should you use a min-heap or a max-heap for top-k?
Use the opposite of what you are collecting. For the k largest items keep a min-heap of size k, so the smallest survivor sits at the root and is the one you evict. For the k smallest, Nil Mamano's blog uses a max-heap of size k and lists it at O(n log k) time and O(k) space. The same blog calls that approach the most space-efficient solution, which is the point to make when the interviewer asks about memory.
Is heapify-then-pop better than a size-k heap?
It depends on whether time or space matters more. The Python documentation says heapify turns a list into a min-heap in place in linear time. Nil Mamano's blog puts heapify-then-pop at O(n + k log n), and Hello Interview puts the size-k heap at O(n log k); in our read, heapify-then-pop is never slower in time but needs O(n) extra space. The size-k heap wins on space and still works on input you cannot hold, so it remains the better default for streams and tight memory.
When is sorting good enough?
Sorting is good enough when the problem needs every element in order, or when k is large. The Python documentation says nlargest and nsmallest perform best when the number of items requested is small; when it is large, sorted() is more efficient. It also says that when n==1, the built-in min() and max() are more efficient than the heap functions.
Can you use heapq or a library priority queue in the interview?
Usually, once you show you know what it does. PracHub's guide says reasonable interviewers allow heapq.nlargest "after you have shown you know what it does internally", and that leading with it is a gamble. Our view: Java's PriorityQueue with your own comparator is normally fine; the risk is a one-line library call that hides the selection logic the interviewer wanted to see.
Your practice plan
A three-week plan for someone working full time:
- Week one, first half. Write a size-k min-heap with (priority, counter, item) tuples from memory, then solve Top K Frequent Elements twice: once with buckets and once with the heap.
- Week one, second half. Solve Top K Largest Elements and K Closest Points to Origin (N log K), and say out loud why buckets fail on the distance key.
- Week two, first half. Build the two-heap running median on Find Median from Data Stream, then merge streams on Merge K Sorted Lists.
- Week two, second half. Solve Sort Elements by Frequency once with a sort and once with buckets, and explain why a size-k heap no longer fits.
- Week three, first half. Solve Meeting Rooms III and Job Scheduler aloud, naming what the heap orders and when items leave it.
- Week three, second half. Attempt GPU Credits II and Meeting Rooms / Interval Scheduling Variants under a timer, and open each answer with the three constraint questions before you write code.
Sources
- TrueInterview question bank — heap and top-K at big-tech companies — counted 2026-10-18
- Top K Frequent Elements: Heap vs Quickselect vs Bucket Sort | PracHub Knowledge Hub — checked 2026-10-18
- Top-K Problems: Sorting vs Heaps vs Quickselect — checked 2026-10-18
- Leetcode 347. Top K Frequent Elements | Hello Interview — checked 2026-10-18
- Quickselect - Wikipedia — checked 2026-10-18
- heapq — Heap queue algorithm — Python 3.15.0 documentation — checked 2026-10-18
- Streaming Top-K Interview Questions: Exact Counts, Sketches, and Memory Budgets | PracHub Knowledge Hub — checked 2026-10-18
Last reviewed: 2026-10-18.