Programming foundations

Stage 4 · Lesson 8 of 10

Space complexity

Data structures and Big-O

measures memory as well as time. Space complexity counts the extra memory your code needs as the input grows, not counting the input itself. A few variables are O(1).

A new list, a set of seen items or a copy of the input is O(n). An n by n grid is O(n²). counts too, because each unfinished call keeps a frame on the .

You often trade one cost for the other. A set makes duplicate checking O(n) time, but it costs O(n) space. Sorting in place and then comparing neighbours takes O(n log n) time with little extra memory.

Stating both costs for every solution, as the milestone asks, is the habit this stage builds.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Step through linked lists, hash tables and sorts with your own input

  • Chapter 5 on lists, dicts, sets and deque: the built-ins you will use from now on

Also worth your time

Mark this stage as not done?