MediumPro challengePythonJavaScriptTypeScript

Min Heap

Data StructuresAlgorithms

Implement a MinHeap class with:

  • push(value) — insert a value.
  • pop() — remove and return the minimum value, or null if empty.
  • peek() — return (but don't remove) the minimum, or null if empty.

solve(ops) replays a sequence of operations and returns each pop/peek
result. push returns null in the output.

Sample tests

Test #1Duplicate values, empty pop returns null
Input: [[["push",1],["push",1],["pop"],["pop"],["pop"]]]
Output: [null,null,1,1,null]
Test #2Push 3,1,2 then pop three times → ascending order
Input: [[["push",3],["push",1],["push",2],["pop"],["pop"],["pop"]]]
Output: [null,null,null,1,2,3]
Test #3Pop from empty heap → null
Input: [[["pop"]]]
Output: [null]
Test #4Push then peek/pop/peek
Input: [[["push",5],["peek"],["pop"],["peek"]]]
Output: [null,5,5,null]
Test #5Peek shows min, pop removes it, new peek shows next
Input: [[["push",10],["push",3],["push",7],["peek"],["pop"],["peek"]]]
Output: [null,null,null,3,3,7]