0Pricing
DSA Interview Prep · Lesson

Heapify, Push, and Pop from Scratch

Implement heapify-up for push and heapify-down for pop, then build a heap from an unsorted array in O(n) using Floyd's algorithm.

Heapify, Push, and Pop from Scratch is a free DSA Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DSA Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Building a MinHeap Class

Implementing a heap from scratch demonstrates mastery of the underlying mechanics and is occasionally asked in senior interviews. A MinHeap class wraps an array and exposes push, pop, peek, and size operations. Internally it maintains the heap property by calling sift-up after push and sift-down after pop. Understanding this implementation makes Python's heapq module completely transparent.

class MinHeap:
    def __init__(self):
        self._data = []

    def push(self, val):
        self._data.append(val)
        self._sift_up(len(self._data) - 1)

    def pop(self):
        if len(self._data) == 1:
            return self._data.pop()
        min_val = self._data[0]
        self._data[0] = self._data.pop()  # move last to root
        self._sift_down(0)
        return min_val

    def peek(self):
        return self._data[0] if self._data else None

    def size(self):
        return len(self._data)

    def _parent(self, i): return (i - 1) // 2
    def _left(self, i):   return 2 * i + 1
    def _right(self, i):  return 2 * i + 2

print('MinHeap class skeleton defined')

Implementing Sift-Up

Sift-up compares a node with its parent and swaps upward as long as the heap property (parent <= child for min-heap) is violated. The key is that the newly inserted element is at the end and bubbles up to its correct position. The while loop runs at most floor(log n) times — the height of the tree. Assign i = parent at each step to continue moving up.

class MinHeap:
    def __init__(self):
        self._data = []

    def _parent(self, i): return (i - 1) // 2
    def _left(self, i):   return 2 * i + 1
    def _right(self, i):  return 2 * i + 2

    def _sift_up(self, i):
        while i > 0:
            p = self._parent(i)
            if self._data[p] > self._data[i]:  # parent > child: swap
                self._data[p], self._data[i] = self._data[i], self._data[p]
                i = p
            else:
                break  # heap property satisfied

    def push(self, val):
        self._data.append(val)
        self._sift_up(len(self._data) - 1)

h = MinHeap()
for v in [5, 3, 8, 1, 4]:
    h.push(v)
print(h._data)  # valid min-heap

Implementing Sift-Down

Sift-down pushes a node downward by repeatedly swapping it with its smallest child (for min-heap), until neither child is smaller or the node reaches a leaf. Always compare against both children and swap with the smaller one to maintain the heap property. Remember to check that child indices are within bounds before comparing values.

def _sift_down(data, i):
    n = len(data)
    while True:
        smallest = i
        l = 2 * i + 1
        r = 2 * i + 2
        if l < n and data[l] < data[smallest]:
            smallest = l
        if r < n and data[r] < data[smallest]:
            smallest = r
        if smallest == i:
            break  # already the smallest among i, l, r
        data[i], data[smallest] = data[smallest], data[i]
        i = smallest

# Test: put a large value at root and sift down
heap = [10, 1, 2, 3, 4, 5, 6]
print('Before sift-down:', heap)
_sift_down(heap, 0)
print('After sift-down:', heap)  # 1 should reach top, 10 sink

Complete MinHeap with Pop

The pop operation removes and returns the root (minimum for min-heap). To maintain the complete binary tree shape, move the last element to the root position, then sift it down. This avoids creating gaps in the array and keeps the representation valid. Edge case: if only one element remains, pop and return it directly without sift-down.

class MinHeap:
    def __init__(self):
        self._data = []

    def push(self, val):
        self._data.append(val)
        i = len(self._data) - 1
        while i > 0:
            p = (i - 1) // 2
            if self._data[p] > self._data[i]:
                self._data[p], self._data[i] = self._data[i], self._data[p]
                i = p
            else: break

    def pop(self):
        if not self._data: return None
        if len(self._data) == 1: return self._data.pop()
        result = self._data[0]
        self._data[0] = self._data.pop()  # last -> root
        i, n = 0, len(self._data)
        while True:
            s, l, r = i, 2*i+1, 2*i+2
            if l < n and self._data[l] < self._data[s]: s = l
            if r < n and self._data[r] < self._data[s]: s = r
            if s == i: break
            self._data[i], self._data[s] = self._data[s], self._data[i]
            i = s
        return result

h = MinHeap()
for v in [5, 3, 8, 1, 4, 2]: h.push(v)
print([h.pop() for _ in range(6)])  # [1,2,3,4,5,8] sorted

Floyd's Heapify Algorithm

Floyd's algorithm builds a min-heap from an unsorted array in O(n) by calling sift-down on every non-leaf node, starting from the last internal node (n//2 - 1) and moving toward the root. Leaves are already trivially valid one-element heaps. The O(n) time bound comes from the fact that most nodes are near the bottom of the tree and only need to sift down a small distance.

def heapify(arr):
    n = len(arr)
    # Start from last non-leaf: index n//2 - 1
    # Work backward to root (index 0)
    for i in range(n // 2 - 1, -1, -1):
        # Sift down node at index i
        j = i
        while True:
            s = j
            l, r = 2*j+1, 2*j+2
            if l < n and arr[l] < arr[s]: s = l
            if r < n and arr[r] < arr[s]: s = r
            if s == j: break
            arr[j], arr[s] = arr[s], arr[j]
            j = s
    return arr

arr = [9, 7, 5, 3, 1, 8, 2, 4, 6]
print('Before:', arr)
heapify(arr)
print('After (min-heap):', arr)  # arr[0] should be 1

Why Floyd's Algorithm is O(n)

The O(n) proof: the tree has n/2^(k+1) nodes at height k. Each node at height k does at most k swaps during sift-down. Total work = sum over all heights k: n/2^(k+1) * k. This geometric series converges to O(n). Contrast with naive one-by-one insertion: each push is O(log n), so n pushes cost O(n log n). Floyd's algorithm is strictly better for batch construction.

import time
import random

# Compare: O(n) heapify vs O(n log n) one-by-one
n = 100000
data = list(range(n, 0, -1))  # reverse sorted = worst case for push

# Method 1: Floyd's O(n)
data1 = data[:]
start = time.time()
for i in range(n // 2 - 1, -1, -1):
    j = i
    while True:
        s = j; l, r = 2*j+1, 2*j+2
        if l < n and data1[l] < data1[s]: s = l
        if r < n and data1[r] < data1[s]: s = r
        if s == j: break
        data1[j], data1[s] = data1[s], data1[j]; j = s
print(f'Floyd heapify: {time.time()-start:.4f}s')

# Method 2: One-by-one insertion
import heapq
start = time.time()
heap = []
for x in data: heapq.heappush(heap, x)
print(f'Push one-by-one: {time.time()-start:.4f}s')

Heap Push into Existing Collection

Python's heapq.heappushpop and heapq.heapreplace are efficient combined operations. heappushpop(heap, item) pushes the new item and immediately pops the smallest — more efficient than two separate calls. heapreplace(heap, item) pops the smallest and pushes the new item in one pass (the new item must be >= the old minimum for correctness). These are useful in top-k streaming algorithms.

import heapq

heap = [1, 3, 5, 7, 9]
heapq.heapify(heap)

# heappushpop: push 2, then pop minimum
# More efficient than push + pop separately
result = heapq.heappushpop(heap, 2)
print('heappushpop(2):', result, '| heap:', heap)

# heapreplace: pop minimum, then push new item
# New item does NOT need to be larger (different from heappushpop)
result2 = heapq.heapreplace(heap, 4)
print('heapreplace(4):', result2, '| heap:', heap)

# Use case: maintaining a fixed-size top-k heap
# heappushpop is the standard pattern

Implementing a MaxHeap from Scratch

A MaxHeap flips the comparison: parent must be greater than or equal to all descendants. Simply invert the comparison in sift-up and sift-down. Alternatively, wrap values in a negation class or negate integers as done with Python's heapq. Implementing from scratch demonstrates that min and max heaps are identical structures with only the comparison operator changed.

class MaxHeap:
    def __init__(self):
        self._data = []

    def push(self, val):
        self._data.append(val)
        i = len(self._data) - 1
        while i > 0:
            p = (i - 1) // 2
            if self._data[p] < self._data[i]:  # FLIP: parent < child = violation
                self._data[p], self._data[i] = self._data[i], self._data[p]
                i = p
            else: break

    def pop(self):
        if not self._data: return None
        if len(self._data) == 1: return self._data.pop()
        result = self._data[0]
        self._data[0] = self._data.pop()
        i, n = 0, len(self._data)
        while True:
            g = i; l, r = 2*i+1, 2*i+2
            if l < n and self._data[l] > self._data[g]: g = l  # FLIP
            if r < n and self._data[r] > self._data[g]: g = r  # FLIP
            if g == i: break
            self._data[i], self._data[g] = self._data[g], self._data[i]; i = g
        return result

h = MaxHeap()
for v in [5, 3, 8, 1, 4, 2]: h.push(v)
print([h.pop() for _ in range(6)])  # [8,5,4,3,2,1]

Delete Arbitrary Element from Heap

Deleting an arbitrary element (not the root) from a heap is O(log n) but requires knowing the element's index. Replace the element with the last element, remove the last, then either sift-up or sift-down the replacement (only one direction will violate the heap property). This technique is used in Dijkstra's algorithm with lazy deletion and in priority queues that support decrease-key operations.

def delete_at_index(heap, i):
    n = len(heap)
    heap[i] = heap[n - 1]
    heap.pop()
    if i >= len(heap):
        return  # deleted the last element
    # Try sift-up first
    p = (i - 1) // 2
    if i > 0 and heap[i] < heap[p]:
        while i > 0:
            p = (i - 1) // 2
            if heap[p] > heap[i]:
                heap[p], heap[i] = heap[i], heap[p]; i = p
            else: break
    else:  # sift down
        j = i; n2 = len(heap)
        while True:
            s = j; l, r = 2*j+1, 2*j+2
            if l < n2 and heap[l] < heap[s]: s = l
            if r < n2 and heap[r] < heap[s]: s = r
            if s == j: break
            heap[j], heap[s] = heap[s], heap[j]; j = s

heap = [1, 3, 2, 7, 4, 5, 6]
print('Before:', heap)
delete_at_index(heap, 2)  # delete element at index 2 (value=2)
print('After:', heap)  # 2 removed, heap still valid

Heap in Top-K Frequent Elements

Top-K Frequent Elements (LeetCode #347) uses a min-heap of size k. Maintain a min-heap where each entry is (frequency, element). Process each unique element: if the heap has fewer than k elements, push; otherwise, if the new element's frequency exceeds the heap's minimum, pop and push. Final heap contains the k most frequent elements in O(n log k) time.

import heapq
from collections import Counter

def top_k_frequent(nums, k):
    count = Counter(nums)
    # Min-heap of (frequency, num)
    heap = []
    for num, freq in count.items():
        heapq.heappush(heap, (freq, num))
        if len(heap) > k:
            heapq.heappop(heap)  # remove least frequent
    return [num for freq, num in heap]

print(top_k_frequent([1,1,1,2,2,3], 2))  # [1, 2]
print(top_k_frequent([4,4,4,3,3,2,1], 2)) # [4, 3]

Heap Applications in Scheduling

Beyond competitive programming, heaps power real-world scheduling systems. Operating system task schedulers use a priority queue (heap) to always run the highest-priority ready process. Event-driven simulations process events in time order using a min-heap keyed on event time. Network packet schedulers prioritise traffic by quality-of-service class. Understanding the heap gives you a mental model for all these systems and comes up naturally in system design interviews about queuing and scheduling.

import heapq

# Simple event-driven simulation using a heap
events = []  # (time, event_description)

def schedule(time, event):
    heapq.heappush(events, (time, event))

def process_next():
    time, event = heapq.heappop(events)
    print(f't={time}: {event}')
    return time, event

# Schedule events out of order:
schedule(10, 'Send email')
schedule(3,  'Open app')
schedule(7,  'Process request')
schedule(1,  'Start server')

# Process in time order:
while events:
    process_next()
# Output: t=1, t=3, t=7, t=10 -- always in time order

Quick Check

Test your understanding of Data Structures & Algorithms — Coding Interview Prep concepts from this lesson.

Lesson Recap

In this lesson you learned: MinHeap and MaxHeap from scratch with sift-up and sift-down, Floyd's O(n) heapify algorithm and why it beats O(n log n) one-by-one insertion, and practical applications including top-k frequent elements and delete-at-index. Next up we explore Python's heapq module and max-heap tricks.

Frequently asked questions

Is the “Heapify, Push, and Pop from Scratch” lesson free?

Yes — the full text of “Heapify, Push, and Pop from Scratch” is free to read here on the web, and the DSA Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DSA Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Heapify, Push, and Pop from Scratch”?

Implement heapify-up for push and heapify-down for pop, then build a heap from an unsorted array in O(n) using Floyd's algorithm. You practise DSA Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DSA Interview Prep?

No prior experience is required. DSA Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Heapify, Push, and Pop from Scratch” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DSA Interview Prep lesson?

Yes. Every DSA Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Heap Property and Array Representation
  2. Heapify, Push, and Pop from Scratch
  3. Python heapq and Max-Heap Tricks
  4. Median from Data Stream and K-Way Merge
← Back to DSA Interview Prep