Design an array-based min heap with insert, extractMin, peek and getSize
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a **min heap** data structure from scratch. A min heap is a complete binary tree where every parent node has a value less than or equal to the values of its children. The root of the tree always contains the minimum element.
The heap should support inserting new elements, extracting (removing) the minimum element, peeking at the minimum element without removal, and querying the current size of the heap.
You must implement the heap using an array-based representation without relying on any built-in heap or priority queue library structures.
Implement the `MinHeap` class:
- `MinHeap()` creates an empty heap.
- `insert(val: int) -> None` inserts a value into the min heap, maintaining the min-heap property.
- `extractMin() -> int` removes and returns the minimum element from the heap. It returns -1 if the heap is empty.
- `peek() -> int` returns the minimum element without removing it. It returns -1 if the heap is empty.
- `getSize() -> int` returns the current number of elements in the heap.
Example 1:
```text
Input:
operations = ["MinHeap", "insert", "insert", "insert", "peek", "extractMin", "peek", "getSize"]
arguments = [[], [3], [1], [2], [], [], [], []]
Output: [null, null, null, null, 1, 1, 2, 2]
```
Create an empty heap and insert 3, 1 and 2. Peek returns 1, the minimum. extractMin removes and returns 1, after which peek returns 2 and the size is 2.
Example 2:
```text
Input:
operations = ["MinHeap", "extractMin", "peek", "insert", "insert", "extractMin", "extractMin", "extractMin"]
arguments = [[], [], [], [5], [10], [], [], []]
Output: [null, -1, -1, null, null, 5, 10, -1]
```
extractMin and peek on the empty heap both return -1. After inserting 5 and 10, extractMin returns 5, then 10, then -1 because the heap is empty again.
```hint Index arithmetic
Store the tree level by level in a plain list. Work out the index of the parent of position i and of its two children, so you can move between levels without node pointers.
```
```hint Restoring the order
Both insert and extractMin break the heap property at exactly one position. Ask which direction that element has to travel, and on the way down, which of the two children it should trade places with.
```
### Constraints & Assumptions
- `1 <= number of operations <= 10^4`
- `-10^9 <= val <= 10^9`
- `extractMin` and `peek` on an empty heap return `-1`.
- No built-in heap or priority queue (for example Python's `heapq` or Java's `PriorityQueue`) may be used.
### Clarifying Questions to Ask
- Since `val` can itself be -1, how should a caller tell an empty-heap -1 from a stored -1? Is the sentinel fixed, or may the method raise or return a null value instead?
- Can the same value be inserted more than once, and should every copy be kept?
- Is a maximum size known up front, or should the backing array grow as needed?
- Will the heap only ever be used from one thread?
### What a Strong Answer Covers
- The mapping between array indices and the complete binary tree (parent and children of index i), and why appending and removing at the end keeps the tree complete.
- Insert by appending and sifting up; extractMin by moving the last element to the root and sifting down toward the smaller child.
- Correct behavior for the empty heap and for extracting the last remaining element.
- Time complexity of each operation and the space used.
- Edge cases such as duplicate values and the -1 sentinel colliding with a real stored value.
### Follow-up Questions
- How would you build a heap from an unsorted array of n values in linear time, and why is that faster than n inserts?
- How would you support decrease-key, or deletion of an arbitrary element?
- How would you turn this into a max heap, or a heap ordered by a custom comparator, without duplicating the code?
- How would you use this heap to keep the k smallest values seen in a stream?
Overview: Implement a min heap from scratch on a plain array, with insert, extractMin, peek and getSize, returning -1 when the heap is empty. It tests the index math of a complete binary tree, sift-up and sift-down, edge cases such as extracting the last element, and the time cost of each operation.