Tags

Backtracking

Backtracking

2 lessons
14 problems
1 question bank
7 community items

backtracking

Algorithms

2 lessons

Backtracking (Intro)

Free
Beginner

60 min

1 prereq

Listing every subset of a 20-element set produces over a million results, and there is no shortcut: you have to enumerate them. The trick is to do it without writing 20 nested loops. Backtracking solves the entire family of "generate all configurations" problems with a single recursive template that walks an implicit decision tree. **Backtracking (Intro)** introduces that template as the "choose, explore, unchoose" pattern. You will see how each recursive frame picks an option, recurses into the resulting state, and then undoes the choice on the way back up so the next branch starts from a clean slate. Classic problems include generating all subsets and permutations, the N-Queens placement puzzle, and a first look at Sudoku. The lesson also introduces pruning, where invalid partial solutions are cut off early so the algorithm explores far less than the full `O(2^n)` or `O(n!)` worst case. In **Recursion Fundamentals**, you learned to define a problem in terms of smaller subproblems. Backtracking adds one more idea: state mutated during recursion has to be restored before returning, so siblings in the call tree see the same starting state. From here, **BFS & DFS (Intro)** generalizes the same tree-of-states view to actual graphs.

Not Started

0%

Algorithms
Backtracking
Recursion
Brute Force
Combinatorics
Problem Solving
Beginner
Free

Branch and Bound

Advanced

50 min

2 prereqs

0/1 Knapsack is exponentially many subsets to consider, but commercial solvers like CPLEX and Gurobi answer instances with thousands of items in seconds. The trick is not better hardware but smarter exploration: at every node of the search tree, compute a cheap upper bound on what any completion of the partial solution could possibly achieve, and prune the entire subtree if that bound cannot beat the best solution found so far. That single discipline of provably-suboptimal pruning is what defines branch and bound. **Branch and Bound** introduces the framework as a refinement of backtracking. You will design bounding functions (typically a relaxation, like fractional knapsack as a bound for 0/1 knapsack), build state-space trees, and choose between three exploration strategies: FIFO (BFS-like), LIFO (DFS-like), and least-cost (priority queue). Classic applications include 0/1 Knapsack with the fractional-relaxation bound, the Traveling Salesman Problem, and the job-assignment problem. The lesson also draws the line between B&B and DP so you know when each is the right approach. In **Backtracking (Intro)**, you walked an implicit decision tree with choose, explore, unchoose. **Greedy (Intro)** taught you that locally optimal choices sometimes give global optima. Branch and bound borrows the search structure of one and the bounding intuition of the other. Next, **Divide and Conquer (Advanced)** turns to algebraic D&C tricks like Karatsuba and Strassen.

Not Started

0%

Algorithms
Branch and Bound
Backtracking
Greedy
Knapsack Problem
Advanced
Premium
Problem Solving

Practice Problems

14 problems

N-Queens II

Not Started
Hard

Given an integer n, return the number of distinct solutions to the n-queens puzzle, where n queens are placed on an n x n chessboard so that no two queens attack each other.

Arrays
Backtracking
Recursion
Algorithms
Advanced

846

5

N-Queens

Not Started
Hard

Place n queens on an n x n chessboard so that no two queens attack each other. Return all distinct board configurations as lists of strings, where 'Q' represents a queen and '.' represents an empty space.

Arrays
Backtracking
Recursion
Algorithms
Advanced

951

7

Word Search II

Not Started
Hard

Given a 2D board of characters and a list of words, find all words that can be formed by sequentially adjacent cells on the board, using each cell at most once per word.

Trie / Prefix Tree
Trie Operations
Backtracking
DFS
Arrays
Strings
Advanced

1.1k

38

Combination Sum II

Not Started
Medium

Given a collection of candidate numbers (which may contain duplicates) and a target, find all unique combinations where the chosen numbers sum to the target. Each number may only be used once.

Arrays
Backtracking
Recursion
Sorting
Algorithms
Intermediate

395

9

Combination Sum

Free
Not Started
Medium

Given an array of distinct integers and a target, return all unique combinations where the chosen numbers sum to the target. Each number may be used an unlimited number of times.

Arrays
Backtracking
Recursion
Algorithms
Intermediate

1.1k

33

Combinations

Not Started
Medium

Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. Return the answer in any order.

Arrays
Backtracking
Recursion
Algorithms
Intermediate

284

5

Generate Parentheses

Free
Not Started
Medium

Given n pairs of parentheses, generate all combinations of well-formed (valid) parentheses.

Stack
Backtracking
Recursion
Parentheses Matching
Intermediate

281

1

Letter Combinations of a Phone Number

Free
Not Started
Medium

Given a string containing digits from 2-9, return all possible letter combinations that the number could represent on a phone keypad.

Strings
Backtracking
Recursion
Algorithms
Intermediate

666

21

Palindrome Partitioning

Not Started
Medium

Given a string, partition it such that every substring of the partition is a palindrome. Return all possible palindrome partitions.

Strings
Backtracking
Recursion
Palindrome
Dynamic Programming
Algorithms
Intermediate

734

21

Path Sum II

Free
Not Started
Medium

Given the root of a binary tree and a target sum, return all root-to-leaf paths where the sum of the values equals the target.

Binary Tree
DFS
Backtracking
Path Problems
Intermediate

1.1k

27

Permutations

Free
Not Started
Medium

Given an array of distinct integers, return all possible permutations in any order.

Arrays
Backtracking
Recursion
Algorithms
Intermediate

291

4

Subsets II

Not Started
Medium

Given an integer array that may contain duplicates, return all possible subsets (the power set) without duplicate subsets.

Arrays
Backtracking
Recursion
Sorting
Algorithms
Intermediate

697

8

Subsets

Free
Not Started
Medium

Given an integer array of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets.

Arrays
Backtracking
Recursion
Algorithms
Intermediate

302

4

Word Search

Free
Not Started
Medium

Given an m x n grid of characters and a string word, determine if the word exists in the grid by following a path of adjacent cells (horizontally or vertically) without reusing any cell.

Arrays
Backtracking
Recursion
DFS
Algorithms
Intermediate

710

2

Community

7 items
Problem
Medium
Free

Restore IP Addresses

Given a string of digits, return every valid IPv4 address that can be formed by inserting three dots, using bounded-depth backtracking.

backtracking
strings
recursion

278

4

4.5 (10)

May 12, 2026

by CodeSnatch

Problem
Hard
$8.99

Word Break II

Return every sentence that can be formed by space-segmenting a string into dictionary words, using memoized DFS keyed by start index.

dynamic-programming
memoization
strings
backtracking

734

12

Apr 12, 2026

by @lunamitchell

Article

Backtracking: The Template That Solves 30 Problems

One choose, explore, unchoose template. Walk it on N-queens, then map the variations: permutations, subsets, word-search, sudoku. The pattern stays put; the predicate moves.

backtracking
recursion
algorithms
graph-traversal-patterns
dfs

241

4

4.2 (15)

Mar 24, 2026

by @lucasmoreau

Problem
Medium
Free

Combination Sum III

Find all unique k-digit combinations from 1-9 that sum to a target n, using cardinality-bounded backtracking.

backtracking
arrays
recursion

458

3

4.5 (10)

Mar 16, 2026

by @tylerperry

Problem
Hard
$9.99

Expression Add Operators

Insert +, -, * between digits of a string so the resulting expression equals a target, using backtracking that handles operator precedence in O(1) per step.

backtracking
strings
math
recursion

516

13

4.3 (13)

Jan 15, 2026

by @jamesmurphy

Problem
Hard
$9.99

Sudoku Solver

Solve a partially-filled 9x9 Sudoku in place using backtracking with row, column, and 3x3-box constraint sets.

backtracking
matrix-algorithms
recursion

404

9

4.2 (11)

Dec 19, 2025

by @riverbanda

Problem
Medium
Free

Beautiful Arrangement

Count the permutations of 1..n where every i either divides perm[i] or is divided by perm[i], using backtracking with divisibility pruning.

backtracking
math
recursion

207

6

4.4 (9)

Dec 2, 2025

by @ethandubois