Latticework

Command Palette

Search for a command to run...

Complexity Analysis

Time vs Space

16 min

Explanation

Algorithms often have more than one valid solution with different tradeoffs — the same problem, solved faster by using more memory, or solved with less memory by taking more time. Detecting a duplicate in a list is a clean example you've already partially seen (in the Data Structures course's Hash Tables module and Python's Performance module):

def has_duplicate_extra_space(nums):
    seen = set()          # extra O(n) memory in the worst case
    for n in nums:
        if n in seen:
            return True     # O(1) average lookup -> O(n) total time
        seen.add(n)
    return False
Try it

Both functions give the same answer for the same input -- the difference is entirely in HOW they get there, not WHAT they return.

Loading editor…
Explanation

Neither approach is universally "better" — it depends on what's scarce:

  • Hash set: O(n) time, O(n) extra space. Best when memory is plentiful and speed matters most.
  • Sort-then-scan: O(n log n) time, no significant extra space beyond the sort itself (and sorted() avoids mutating the original list, at the cost of a copy — sorting the list IN PLACE with .sort() instead would use less memory still).

This tradeoff — trading memory for speed, or speed for memory — shows up constantly: caching (memory for speed), streaming algorithms (speed/ correctness for memory), compression (time for space). Recognizing "this is a time/space tradeoff" is often the first step to picking the right one for your actual constraints.

Exercise

Write `has_duplicate_extra_space(nums)`: detect a duplicate value in O(n) TIME, using a set (O(n) extra SPACE) to track values seen so far.

Exercise

Write `has_duplicate_no_extra_space(nums)`: detect a duplicate value using O(n log n) TIME instead (sort a copy of the list, then scan for adjacent equal values), trading away the O(n) hash-set memory for extra time instead.

Quiz

What's the fundamental tradeoff between the hash-set approach (O(n) time) and the sort-then-scan approach (O(n log n) time) to duplicate detection?

Checkpoint

You can implement the same algorithm two different ways with different time/space tradeoffs, and can articulate what's being traded for what.