Algorithms

Backtracking

๐Ÿ“‹ At a Glance

AspectDetails
Time to Read35 minutes
PrerequisitesPart 2 (Recursion), Part 16 (DP basics)
Key ConceptsBacktracking Template, Pruning, N-Queens, Sudoku, Branch & Bound
Difficultyโญโญโญ (Intermediate)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    BACKTRACKING                                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  EXPLORE โ†’ CHOOSE โ†’ EXPLORE โ†’ BACKTRACK                         โ”‚
โ”‚                                                                 โ”‚
โ”‚              โ—‹ root                                             โ”‚
โ”‚            / | \                                                โ”‚
โ”‚           โ—‹  โ—‹  โ—‹       Level 1: choices                        โ”‚
โ”‚          /|  |  |\                                              โ”‚
โ”‚         โ—‹ โ—‹  ร—  โ—‹ โ—‹     Level 2: ร— = pruned (invalid)           โ”‚
โ”‚        /|    |    |\                                            โ”‚
โ”‚       โ—‹ ร—    โ—‹    ร— โ—‹   Level 3: leaves = solutions             โ”‚
โ”‚                                                                 โ”‚
โ”‚  Key insight: Don't explore paths that can't lead to solution   โ”‚
โ”‚                                                                 โ”‚
โ”‚  Template:                                                      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚ backtrack(state):                                       โ”‚    โ”‚
โ”‚  โ”‚   if isComplete(state):                                 โ”‚    โ”‚
โ”‚  โ”‚     addToResults(state)                                 โ”‚    โ”‚
โ”‚  โ”‚     return                                              โ”‚    โ”‚
โ”‚  โ”‚                                                         โ”‚    โ”‚
โ”‚  โ”‚   for choice in getChoices(state):                      โ”‚    โ”‚
โ”‚  โ”‚     if isValid(choice):                                 โ”‚    โ”‚
โ”‚  โ”‚       makeChoice(choice)                                โ”‚    โ”‚
โ”‚  โ”‚       backtrack(state)                                  โ”‚    โ”‚
โ”‚  โ”‚       undoChoice(choice)  โ† KEY STEP                    โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ What You'll Learn

After completing this article, you will be able to:

  1. Apply the backtracking template: Systematic approach for any problem
  2. Solve classic problems: N-Queens, Sudoku, permutations
  3. Prune effectively: Cut search space dramatically
  4. Use branch and bound: Optimization with bounds
  5. Choose between approaches: Backtracking vs DP vs brute force

๐Ÿ”ฅ Production Story: The Configuration Solver

A network equipment company needed to find valid configurations for complex hardware setups with many constraints.

The Problem

JAVA(15 lines)
Code
Loading syntax highlighter...

The Solution: Backtracking with Constraint Propagation

JAVA(48 lines)
Code
Loading syntax highlighter...

The Impact

MetricBrute ForceBacktracking
Search space10^50~10^6 (pruned)
Time for solutionImpossible0.3 seconds
MemoryN/AO(depth)
Valid configs found0847

๐Ÿง  Mental Model: The Maze Explorer

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              BACKTRACKING AS MAZE EXPLORATION                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”                                          โ”‚
โ”‚  โ”‚ S โ”‚   โ”‚   โ”‚ โ–ˆ โ”‚   โ”‚   S = Start                              โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค   E = End (goal)                         โ”‚
โ”‚  โ”‚   โ”‚ โ–ˆ โ”‚   โ”‚   โ”‚   โ”‚   โ–ˆ = Wall (invalid)                     โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค   ยท = Path tried                         โ”‚
โ”‚  โ”‚   โ”‚   โ”‚ โ–ˆ โ”‚   โ”‚   โ”‚   โ—‹ = Current position                   โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค                                          โ”‚
โ”‚  โ”‚ โ–ˆ โ”‚   โ”‚   โ”‚ โ–ˆ โ”‚   โ”‚                                          โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค                                          โ”‚
โ”‚  โ”‚   โ”‚   โ”‚   โ”‚   โ”‚ E โ”‚                                          โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”˜                                          โ”‚
โ”‚                                                                 โ”‚
โ”‚  Algorithm:                                                     โ”‚
โ”‚  1. From current position, try each direction                   โ”‚
โ”‚  2. If direction leads to wall or visited โ†’ skip (prune)        โ”‚
โ”‚  3. Move to new position, mark as visited                       โ”‚
โ”‚  4. If at goal โ†’ found solution!                                โ”‚
โ”‚  5. If stuck โ†’ go back (backtrack), try next direction          โ”‚
โ”‚                                                                 โ”‚
โ”‚  Key: Marking visited prevents infinite loops                   โ”‚
โ”‚       Backtracking undoes the "visited" mark                    โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ฌ Deep Dive: The Backtracking Template

General Template

JAVA(35 lines)
Code
Loading syntax highlighter...

Subsets (Power Set)

JAVA(20 lines)
Code
Loading syntax highlighter...

Permutations

JAVA(59 lines)
Code
Loading syntax highlighter...

Combinations

JAVA(51 lines)
Code
Loading syntax highlighter...

๐Ÿ”ฌ Deep Dive: N-Queens Problem

The Problem

Place N queens on an Nร—N chessboard so no two attack each other.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    N-QUEENS (N=4)                               โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  Solution 1:          Solution 2:                               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”                         โ”‚
โ”‚  โ”‚   โ”‚ Q โ”‚   โ”‚   โ”‚    โ”‚   โ”‚   โ”‚ Q โ”‚   โ”‚                         โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค    โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค                         โ”‚
โ”‚  โ”‚   โ”‚   โ”‚   โ”‚ Q โ”‚    โ”‚ Q โ”‚   โ”‚   โ”‚   โ”‚                         โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค    โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค                         โ”‚
โ”‚  โ”‚ Q โ”‚   โ”‚   โ”‚   โ”‚    โ”‚   โ”‚   โ”‚   โ”‚ Q โ”‚                         โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค    โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค                         โ”‚
โ”‚  โ”‚   โ”‚   โ”‚ Q โ”‚   โ”‚    โ”‚   โ”‚ Q โ”‚   โ”‚   โ”‚                         โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”˜                         โ”‚
โ”‚                                                                 โ”‚
โ”‚  Constraint: No two queens share row, column, or diagonal       โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Implementation

JAVA(84 lines)
Code
Loading syntax highlighter...

๐Ÿ”ฌ Deep Dive: Sudoku Solver

Implementation

JAVA(109 lines)
Code
Loading syntax highlighter...

๐Ÿ”ฌ Deep Dive: Branch and Bound

The Concept

Branch and Bound adds bounding functions to prune branches that can't improve on the best solution found so far.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                 BRANCH AND BOUND                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  Standard Backtracking:                                         โ”‚
โ”‚  - Prunes invalid solutions                                     โ”‚
โ”‚                                                                 โ”‚
โ”‚  Branch and Bound (for optimization):                           โ”‚
โ”‚  - Also prunes solutions that can't beat current best           โ”‚
โ”‚                                                                 โ”‚
โ”‚  Components:                                                    โ”‚
โ”‚  1. Branch: Split problem into subproblems                      โ”‚
โ”‚  2. Bound: Calculate optimistic estimate for subtree            โ”‚
โ”‚  3. Prune: If bound โ‰ค best found, skip this branch              โ”‚
โ”‚                                                                 โ”‚
โ”‚  Example for minimization:                                      โ”‚
โ”‚                                                                 โ”‚
โ”‚        Current best = 50                                        โ”‚
โ”‚              โ—‹                                                  โ”‚
โ”‚           /  |  \                                               โ”‚
โ”‚          โ—‹   โ—‹   โ—‹                                              โ”‚
โ”‚      lb=30 lb=55 lb=40                                          โ”‚
โ”‚        โ†“    โœ—     โ†“     lb=55 > 50, prune!                      โ”‚
โ”‚      explore   explore                                          โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

TSP with Branch and Bound

JAVA(84 lines)
Code
Loading syntax highlighter...

0/1 Knapsack with Branch and Bound

JAVA(70 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Mistakes

Mistake 1: Forgetting to Undo Choice

JAVA(17 lines)
Code
Loading syntax highlighter...

Mistake 2: Modifying Loop Variable

JAVA(17 lines)
Code
Loading syntax highlighter...

Mistake 3: Adding Reference Instead of Copy

JAVA(17 lines)
Code
Loading syntax highlighter...

๐Ÿ› Debug This: Permutation Bug

This permutation generator has a bug. Can you find it?

JAVA(21 lines)
Code
Loading syntax highlighter...
๐Ÿ” Click to reveal the bug
Bug: Using contains(nums[i]) to check if element is used. This fails when array has duplicates!
JAVA(22 lines)
Code
Loading syntax highlighter...
Example where bug manifests:
nums = [1, 1, 2]
Bug: "1 is already in current" - skips valid permutations
Result: Missing [1,1,2], [1,2,1], [2,1,1]

๐Ÿ’ป Exercises

Exercise 1: Generate Parentheses โญโญ

Generate all valid combinations of n pairs of parentheses.

JAVA(3 lines)
Code
Loading syntax highlighter...

Exercise 2: Letter Combinations of Phone โญโญ

Given digits 2-9, return all letter combinations (like phone keypad).

JAVA(3 lines)
Code
Loading syntax highlighter...

Find if word exists in 2D grid (adjacent cells, no reuse).

JAVA(3 lines)
Code
Loading syntax highlighter...

Exercise 4: Palindrome Partitioning โญโญโญ

Partition string so every substring is a palindrome.

JAVA(3 lines)
Code
Loading syntax highlighter...

Exercise 5: Expression Add Operators โญโญโญโญ

Add +, -, * between digits to reach target.

JAVA(3 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Questions

Q1: "What's the difference between backtracking and DFS?"

Answer:

DFS is a traversal technique. Backtracking uses DFS but with:

  1. State modification: Change state before recursing
  2. State restoration: Undo changes after recursing
  3. Pruning: Skip invalid branches early
JAVA(20 lines)
Code
Loading syntax highlighter...

Q2: "How do you optimize backtracking?"

Answer:
  1. Pruning: Skip invalid branches early
    JAVA
    Code
    Loading syntax highlighter...
  2. Ordering: Try most constrained choice first
    JAVA
    Code
    Loading syntax highlighter...
  3. Constraint propagation: Reduce choices after each decision
    JAVA
    Code
    Loading syntax highlighter...
  4. Symmetry breaking: Avoid equivalent solutions
    JAVA
    Code
    Loading syntax highlighter...
  5. Memoization: Cache repeated subproblems
    JAVA
    Code
    Loading syntax highlighter...

Q3: "When should I use backtracking vs DP?"

Answer:
BacktrackingDP
Find all solutionsFind optimal/count
Solutions have structure (permutation, subset)Overlapping subproblems
Constraints are complexState can be compactly represented
O(b^d) where b=branching, d=depthPolynomial if states are polynomial
Rule of thumb:
  • Need all solutions โ†’ Backtracking
  • Need count/optimal โ†’ Try DP first
  • Can't define subproblems โ†’ Backtracking

Q4: "Explain the N-Queens problem and optimize it."

Answer:
JAVA(27 lines)
Code
Loading syntax highlighter...

Q5: "How does branch and bound improve over basic backtracking?"

Answer:
Branch and bound adds bounding functions for optimization problems:
Backtracking: Prune if state is INVALID
Branch & Bound: Prune if state CAN'T IMPROVE on best found

For minimization:
- Calculate lower bound on solutions in this subtree
- If lower_bound >= best_found, prune entire subtree

For maximization:
- Calculate upper bound
- If upper_bound <= best_found, prune

Example (TSP):
- Current path cost: 50
- Lower bound for remaining: 30 (sum of minimum edges)
- Total lower bound: 80
- Best complete tour found: 75
- 80 > 75 โ†’ prune this branch!

๐Ÿ“‹ Quick Reference

Problem Types

ProblemKey Insight
SubsetsInclude/exclude each element
PermutationsTrack used elements
CombinationsUse start index to avoid duplicates
N-QueensColumn + 2 diagonal checks
SudokuRow + column + box constraints

Complexity

ProblemTimeSpace
SubsetsO(2^n)O(n)
PermutationsO(n!)O(n)
Combinations(n,k)O(C(n,k))O(k)
N-QueensO(n!)O(n)
SudokuO(9^(empty cells))O(1)

Template Summary

JAVA(14 lines)
Code
Loading syntax highlighter...

๐Ÿ”— What's Next?

In Part 21: Probabilistic & Approximation Algorithms, we'll explore:
  • Randomized algorithms
  • Bloom filters
  • HyperLogLog
  • Approximation algorithms

๐Ÿ“… Review Schedule

DayFocusTime
0Full read + subsets/permutations90 min
1Quick Reference10 min
3N-Queens implementation25 min
7Sudoku solver30 min
14Debug exercise + B&B20 min
30Interview questions15 min