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
PROBLEM-SOLVING TECHNIQUES

Backtracking

Build a solution one choice at a time, and undo choices that can’t work.

Advanced10 problems3 interactive subtopicsO(n × 2ⁿ) time · O(n) stack
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Record the empty set
record [ ]
[ ]
[1]
[2]
[3]
[1,2]
[1,3]
[2,3]
[1,2,3]
  • current
  • not reached yet
  • visited / done
  • result
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [1, 2, 3]
1def subsets(nums):
2 result, path = [], []
3 def backtrack(start):
▶4 result.append(path[:])
5 for i in range(start, len(nums)):
6 path.append(nums[i])
7 backtrack(i + 1)
8 path.pop()
9 backtrack(0)
10 return result
STEP 1 OF 5Setup

Record the empty set

Every call first saves a copy of the current path.

VARIABLES

path
[]
results so far
1
02

Intuition & complexity

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

What it is

Backtracking explores a tree of decisions with recursion: choose, recurse, then un-choose. Pruning skips branches that can’t lead to a valid answer.

Why it matters

Problems that ask for all combinations, permutations or valid arrangements need a systematic search.

Think of it like this

Solving a maze by always taking a path and walking back to the last junction at a dead end.

Use it when

  • Generating all subsets, permutations or combinations
  • Constraint puzzles such as N-Queens or Sudoku
  • Small inputs where exponential work is acceptable

Avoid it when

  • You only need a count or best value and subproblems repeat - use DP
  • Large n (beyond about 20) without strong pruning

Complexity cheatsheet

OperationTimeSpace
Subsets of n itemsO(n × 2ⁿ)O(n) stack
Permutations of n itemsO(n × n!)O(n) stack
With good pruningoften far lessO(n)

Often confused with: dynamic programming - it also explores choices, but reuses results when subproblems repeat.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

all possiblecombinationspermutationsgenerateN-Queens
  • n ≤ 10 to 20 often signals that exponential search is expected
  • “Return all” means the output itself can be exponential
04

Mistakes & edge cases

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

Common mistakes

  • Appending path instead of a copy - every result ends up as the same list
  • Forgetting to undo the choice with path.pop()
  • Not skipping duplicates for inputs like [1, 2, 2]

Edge cases to test

  • Empty input - one subset, the empty one
  • Duplicate values
  • A target that can’t be reached in combination-sum problems
Interview tip: Draw the first two levels of the decision tree - it makes the recursion and the pruning obvious.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Why append path[:] instead of path?

06

Practice

Apply what you just watched on real interview problems.

Generate Backtracking problem in AI DSA Practice →
  • BeginnerSubsetsLeetCode 78 ↗ (opens in a new tab)
  • IntermediateCombination SumLeetCode 39 ↗ (opens in a new tab)
  • AdvancedN-QueensLeetCode 51 ↗ (opens in a new tab)
Monotonic QueueGreedy Algorithms