Core · Module 7 · Lesson 4 of 4
Backtracking basics
Backtracking builds a solution one choice at a time, explores each choice recursively and undoes it afterwards. Pruning dead ends early is what makes it practical.
By the end you can
- Write a backtracking search with the choose, explore, unchoose pattern
- Generate subsets, permutations and combinations, and count how many there are
- Prune branches that can't lead to a solution, and estimate the cost of a search
Builds on Recursion vs iteration.
01 · A question first
There are 4.4 billion ways to put 8 queens on a chessboard. Only 92 of them leave no two queens attacking each other. A backtracking search finds all 92 after trying only 15,720 queen placements. How does it rule out the other billions without looking at them?
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- Solving by trying
- Choose, explore, unchoose
- Subsets, permutations, combinations
- Pruning
- Backtracking in real software
- Pseudocode & Multi-Language Implementations (C++, Java, Python)
- Complexity Derivation, Edge Cases & Common Mistakes
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem