Level 1: Bronze
A memorized definition. Not wrong, but it stops there.
A structure that quickly returns the max or min.
2 stars Asked often.
In one or two sentences
A heap is a complete binary tree in which every parent is larger (max heap) or smaller (min heap) than its children. It implements a priority queue that quickly returns the largest or smallest item.
Say it out loud, or type it below. What you type never leaves this device.
A memorized definition. Not wrong, but it stops there.
A structure that quickly returns the max or min.
Correct, but too broad or missing context.
In a min heap the top is the smallest value, so reading it is O(1), and insert or remove repairs the shape in O(log n). A priority queue is built on a heap.
Reaches principle, use and trade-off, with words that invite the next question.
Because it is a complete binary tree it fits in one array, with parent and child positions computed from the index. Insert appends and sifts up. Remove takes the top, moves the last element up there and sifts down. Unlike a binary search tree it guarantees only the top, not full order, so finding an arbitrary value is O(n). It serves top k queries, job scheduling and Dijkstra's algorithm.
These are likely next questions from an interviewer. Think of your answer first, then press the button.
1 / 3
How long does it take to build a heap from an array?
O(n) with the bottom up method. Inserting one by one is O(n log n).
2 / 3
How do a heap and a binary search tree differ?
A heap keeps only the parent child order, with none between siblings. Max or min is fast, but searching for an arbitrary value is slow.
3 / 3
How do you find the top k?
Keep a min heap of size k, insert each element and pop the smallest when it overflows. What remains is the top k in O(n log k).
This is a textbook level concept, so no outside source is listed.