Skip to content
intermediate

Backtracking

Explore constrained choices recursively, prune invalid paths, and undo state cleanly.

Problems

Problems in Backtracking

Practice problems that share this primary solution pattern and compare the clues that reveal it.

Dark-themed laptop setup with a red glowing keyboard and code on screen, ideal for tech enthusiasts.
intermediate
10 min read

Combination Sum

Treat this as an enumeration problem, not a permutation problem. Sort the candidates, keep combinations in nondecreasing order, recurse from the same index…

View solution
Overhead view of a MacBook laptop on a dark desk, showcasing modern technology and minimalism.
intermediate
12 min read

Combination Sum II

The hard part is not finding combinations that add to the target. It is finding them once while respecting the physical number of occurrences in the input.

View solution
Illustration of a stock market chart with red and green data, showing market trends and analytics.
intermediate
10 min read

Generate Parentheses

Generate only prefixes that can still become valid. The balance state tells you exactly which branches to keep.

View solution
System with various wires managing access to centralized resource of server in data center
expert
14 min read

Sudoku Solver

The phrase “try digits and backtrack” is the easy part. The interview-grade solution keeps four representations synchronized: the board, row constraints,…

View solution
3D rendered abstract brain concept with neural network.
intermediate
11 min read

Combinations

The duplicate-ordering trap is the whole problem: [1, 2] and [2, 1] represent one selection, not two. Build every path in increasing order, and the…

View solution
3D abstract geometric structure with gold lines and black polygons on a dark background.
advanced
13 min read

N-Queens

The board is only the output surface. The real N-Queens solution is a depth-n search over column assignments, with three constraints checked before each…

View solution
Close-up of interlocking metal gears creating an industrial texture.
advanced
13 min read

N-Queens II

The key change from N-Queens is the output contract: you need one integer, so the search should retain only reversible constraints and count valid leaves.

View solution
Detailed view of rough and dry brown soil, showcasing its natural texture.
intermediate
11 min read

Permutations

The search tree is easy to recognize and easy to corrupt. Build one position at a time, choose an unused value, recurse, then undo exactly that choice.

View solution
A stack of traditional terracotta pots in a Vietnamese pottery workshop, illustrating local craftsmanship.
intermediate
12 min read

Permutations II

When nums = [1, 1, 2], ordinary permutation backtracking treats the two 1 values as different input positions. That creates duplicate value sequences.

View solution
Close-up of dual computer monitors with green coding interfaces in a dark room, highlighting cyber security themes.
intermediate
10 min read

Restore IP Addresses

The trap is to think “place three dots.” The useful model is narrower: choose exactly four contiguous digit segments, validate each one immediately, and…

View solution
A breathtaking view of a desert landscape with a vibrant sunset illuminating the horizon.
intermediate
12 min read

Subsets II

The duplicate bug comes from treating equal input positions as different decisions. Sort first, then skip equal candidates only when they are siblings at…

View solution
A child actively assembling a robotics project with electronic components, showcasing technology education.
intermediate
14 min read

Word Search

A grid DFS can match the right letters and still be wrong. The missing piece is path-local state: mark a cell when you enter it, explore from that choice,…

View solution