Stage 4 · Lesson 8 of 10
Space complexity
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
- CoursePaidGrokking Data Structures & Algorithms for Coding Interviews
Design Gurus · Beginner · Subscription
Each structure built from first principles, with solutions in Python, Java, C++, JavaScript and C#
- CourseFreeCS50: Introduction to Computer Science
Harvard University · Beginner
Free: weeks 3 and 5 cover searching, sorting, linked lists, hash tables and trees
Practise
Keep handy
Also worth your time
Time and space complexity worked through five common algorithms