Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
FOUNDATIONS

Time & Space Complexity

Estimate how work and memory grow with input size before you write a line.

Beginner6 problems3 interactive subtopicsO(n) time · O(n) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/4Create an empty set
seen = set()
01234
3
1
4
1
5
SEEN
(empty)
  • active this step
  • visited / done
  • result
Space Play←→ StepR Replay
1 / 4
PYTHONnums = [3, 1, 4, 1, 5]
1def has_duplicate(nums):
▶2 seen = set()
3 for x in nums:
4 if x in seen:
5 return True
6 seen.add(x)
7 return False
STEP 1 OF 4Setup

Create an empty set

seen will grow to at most n items, so this solution uses O(n) extra memory.

VARIABLES

x
-
seen
set()
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Big-O describes how running time or memory grows as the input size n grows, ignoring constant factors.

Why it matters

It lets you compare solutions, and constraints like n ≤ 10⁵ tell you which complexities will pass.

Think of it like this

Reading every page to find a word is O(n); jumping to the index at the back is closer to O(log n).

Use it when

  • Choosing between two working solutions
  • Reading constraints to rule out brute force
  • Explaining time vs memory trade-offs

Avoid it when

  • Treating Big-O as exact runtime - constants still matter for small n
  • Ignoring hidden costs like slicing or string += inside loops

Complexity cheatsheet

OperationTimeSpace
Loop over n itemsO(n)O(1)
Nested loop over all pairsO(n²)O(1)
Sort, then scanO(n log n)O(n)

Often confused with: average vs worst case - dict lookups are O(1) on average but O(n) in the worst case.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

n ≤ 10⁵time limitoptimizecan you do better?extra space
  • About 10⁸ simple operations per second is a common rule of thumb
  • n ≤ 20 often allows O(2ⁿ); n ≤ 10⁵ needs O(n log n) or better
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Calling a nested loop O(n) because the inner loop “looks small”
  • Forgetting that slicing (nums[1:]) or string += in a loop copies data
  • Leaving out the space used by recursion depth or extra sets

Edge cases to test

  • Empty input - make sure the return value is still right
  • All elements equal - the worst case for some algorithms
  • Very large n - recursion can hit Python’s default limit of 1000 frames
Interview tip: State both time and space, and say whether you mean the average or the worst case.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

A loop starts at i = 1 and doubles i until i ≥ n. What is its time complexity?

06

Practice

Apply what you just watched on real interview problems.

Generate Array problem in AI DSA Practice →
  • BeginnerContains DuplicateLeetCode 217 ↗ (opens in a new tab)
  • IntermediateProduct of Array Except SelfLeetCode 238 ↗ (opens in a new tab)
  • AdvancedLongest Consecutive SequenceLeetCode 128 ↗ (opens in a new tab)
Arrays