Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Backtracking Patterns
DSA

Backtracking Patterns

Learn the core backtracking framework for exploring choices, undoing decisions, and finding valid solutions.

Master Backtracking Patterns for DSA + Competitive Programming

Backtracking explores all valid configurations by building a solution incrementally and abandoning paths that violate constraints.

“Try → Explore → Undo” — the three-step dance of constraint satisfaction.


Pattern Table

PatternTypical QuestionsKeywords / Detection Cues
Constraint SatisfactionN-Queens, Sudokuvalid, constraint, arrangement
Grid BacktrackingWord Search, Rat in Mazegrid, direction, path found
Pruning & OptimizationPermutations with duplicatesprune, branch & bound, skip duplicates
Subset / Combination GenerationSubsets II, combination sumduplicates, skip, sort

Mental Trigger

Invalid? Prune. Complete? Record. Otherwise → Try each choice → Recurse → Undo.


Generic Java Backtracking Template (Base)

public void backtrack(State state) {
    if (isInvalid(state)) return;
    if (isComplete(state)) {
        saveSolution(state);
        return;
    }

    for (Choice choice : getChoices(state)) {
        makeChoice(state, choice);
        backtrack(state);
        undoChoice(state, choice);      // backtrack step
    }
}

Everything in Backtracking is try → explore → undo with constraints.


Recognition Cheat Sheet

If you see…Think…
All valid configurationsConstraint satisfaction
Grid + path existsGrid backtracking
Find all with constraintsBacktracking + pruning
Skip invalid earlyPruning

My Private Notes

Notes are auto-saved locally to this device.