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

    Foundations: measuring code · Lesson 2 of 23

    Space and the time–memory trade-off

    How much memory a solution uses, and when to spend memory to save time.

    9 minutes3 games

    Besides steps, we count the extra memory a solution needs beyond the input. A Set of every element is O(n) space; a handful of variables is O(1).

    In many problems we can buy time with memory: store something in a Map so it's never recomputed.

    JavaScript
    // Pair sum: two indexes whose values add up to target
    // Version 1: O(n²) time, O(1) space
    function pairSlow(nums, target) {
      for (let i = 0; i < nums.length; i++)
        for (let j = i + 1; j < nums.length; j++)
          if (nums[i] + nums[j] === target) return [i, j]
      return []
    }
    
    // Version 2: O(n) time, O(n) space — remember what we've seen
    function pairFast(nums, target) {
      const where = new Map()
      for (let i = 0; i < nums.length; i++) {
        const need = target - nums[i]
        if (where.has(need)) return [where.get(need), i]
        where.set(nums[i], i)
      }
      return []
    }
    
    console.log(pairSlow([2, 7, 11, 15], 9), pairFast([2, 7, 11, 15], 9))
    SolutionTimeSpaceWhen to choose it

    pairSlow

    O(n²)

    O(1)

    Very tight memory and small n

    pairFast

    O(n)

    O(n)

    Almost always

    Sort + two pointers

    O(n log n)

    O(1)–O(n)

    When original indexes don't matter

    Recursion uses memory too

    Every function call holds a slot on the call stack until it returns. A recursive function that goes n levels deep uses O(n) memory even without building arrays, which is why large inputs can hit "Maximum call stack size exceeded".

    Important

    Amortized O(1): a JavaScript push sometimes copies the array to a bigger block (O(n)), but rarely, because the capacity doubles each time. Spread over all pushes the average is O(1), hence O(1) amortized. unshift, though, is always O(n) since it shifts every element.

    Quick question

    A function builds a new array of the squares of all numbers. Its extra space?

    Spot the bug

    This should sum 1 + 2 + … + n, but crashes when n = 100000. Which line is the cause?

    Interview question

    The interviewer says "your solution uses O(n) memory; can you make it O(1)?" How do you think about it?

    Exercise

    Write sumTo(n) with a plain loop (O(1) memory) instead of recursion. It must print 5000050000.

    Expected output: 5000050000

    JavaScript
    function sumTo(n) {
      
    }
    
    console.log(sumTo(100000))

    Practise on real problems

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

    • Pair with target sumEasy
    • Zeros to the endEasy
    • Product of everything elseMedium

    Learn more from

    • Wikipedia: Space complexityen.wikipedia.org
    • Wikipedia: Amortized analysisen.wikipedia.org
    • MDN: Mapdeveloper.mozilla.org

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousBig-O: how code growsNextArrays and strings under the hood
    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.