Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    DSADSA

    Foundations: measuring code

    • Big-O: how code grows
    • Space and the time–memory trade-off
    • Arrays and strings under the hood
    • Hash maps and sets
    • How to approach any interview problem

    Array patterns

    • Two pointers
    • Sliding window
    • Prefix sums
    • Binary search, done right
    • Sorting: merge sort and quicksort

    Stacks, queues and linked lists

    • Stacks and monotonic stacks
    • Queues and deques
    • Linked lists

    Recursion and trees

    • Recursion without fear
    • Backtracking: try and undo
    • Binary trees
    • Binary search trees
    • Heaps and priority queues

    Graphs and dynamic programming

    • Graphs: BFS and DFS
    • Shortest paths, topological sort and union-find
    • Dynamic programming: the idea
    • DP on grids and strings
    • Greedy: the best choice right now
    DSA

    Recursion and trees · Lesson 18 of 23

    Heaps and priority queues

    The smallest or largest item in O(1), insert and remove in O(log n), and "top k" problems.

    15 minutes2 games

    A heap is a complete binary tree stored in an array with one rule: each parent is less than or equal to its children (a min-heap). So the smallest item is always heap[0].

    • Peek at the smallest: O(1).
    • Insert or remove the smallest: O(log n).

    That's exactly a priority queue: a line that serves the most important item first (tasks, Dijkstra, merging k lists).

    heap =

    1[0]
    3[1]
    2[2]
    7[3]
    4[4]
    5[5]

    Item i has children at 2i + 1 and 2i + 2 and its parent at floor((i - 1) / 2). No pointers: arithmetic is enough.

    JavaScript
    // JavaScript has no built-in heap, so interviews often ask you to write one
    class MinHeap {
      constructor() { this.a = [] }
      get size() { return this.a.length }
      peek() { return this.a[0] }
      push(x) {
        const a = this.a
        a.push(x)
        let i = a.length - 1
        while (i > 0) {                          // bubble up while smaller than the parent
          const p = (i - 1) >> 1
          if (a[p] <= a[i]) break
          ;[a[p], a[i]] = [a[i], a[p]]
          i = p
        }
      }
      pop() {
        const a = this.a, top = a[0], last = a.pop()
        if (a.length) {
          a[0] = last
          let i = 0
          for (;;) {                             // sink down to the smaller child
            const l = 2 * i + 1, r = l + 1
            let m = i
            if (l < a.length && a[l] < a[m]) m = l
            if (r < a.length && a[r] < a[m]) m = r
            if (m === i) break
            ;[a[m], a[i]] = [a[i], a[m]]
            i = m
          }
        }
        return top
      }
    }
    
    const h = new MinHeap()
    for (const x of [5, 3, 8, 1, 9, 2]) h.push(x)
    const out = []
    while (h.size) out.push(h.pop())
    console.log(out.join(" "))

    Top k: a small heap of size k

    Want the largest k of a million items? Don't sort them all (O(n log n)). Keep a min-heap of size k: push each item, and if the size exceeds k, pop the smallest. At the end the heap holds the top k and peek() is the kth largest. Cost: O(n log k).

    Important

    The inverted trick confuses many people: the largest k use a min-heap (to discard the smallest quickly), and the smallest k use a max-heap. To get a max-heap from a min-heap, store negated values (-x), as is common with Python's heapq.

    Quick question

    You need the 10 largest numbers from a stream of a billion, with limited memory. The best approach?

    Interview question

    How do you get the median of a growing stream of numbers at any moment?

    Exercise

    Use MinHeap (included) to write kthLargest(nums, k) with a heap of size k. It must print 5 4.

    Expected output: 5 4

    JavaScript
    class MinHeap {
      constructor() { this.a = [] }
      get size() { return this.a.length }
      peek() { return this.a[0] }
      push(x) { const a = this.a; a.push(x); let i = a.length - 1; while (i > 0) { const p = (i - 1) >> 1; if (a[p] <= a[i]) break; [a[p], a[i]] = [a[i], a[p]]; i = p } }
      pop() { const a = this.a, top = a[0], last = a.pop(); if (a.length) { a[0] = last; let i = 0; for (;;) { const l = 2 * i + 1, r = l + 1; let m = i; if (l < a.length && a[l] < a[m]) m = l; if (r < a.length && a[r] < a[m]) m = r; if (m === i) break; [a[m], a[i]] = [a[i], a[m]]; i = m } } return top }
    }
    
    function kthLargest(nums, k) {
      
    }
    
    console.log(kthLargest([3, 2, 1, 5, 6, 4], 2), kthLargest([3, 2, 3, 1, 2, 4, 5, 5, 6], 4))

    Practise on real problems

    Practice problems that use what you learned here. Solve them in JavaScript, Python or C++.

    • Kth largest elementMedium
    • Last stone weightEasy
    • K closest points to the originMedium
    • Top k most frequentMedium
    • Task scheduler with cooldownMedium

    Learn more from

    • Princeton Algorithms: Priority Queuesalgs4.cs.princeton.edu
    • Wikipedia: Binary heapen.wikipedia.org
    • VisuAlgo: Binary Heapvisualgo.net

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousBinary search treesNextGraphs: BFS and DFS
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.