BTI Beyond The Interviews

DSA · 9 sections

Backtracking

DFS with undo — permutations, combinations, N-queens, Sudoku, word search, and partitioning problems.

Premium · preview 118 cards Updated 24 Oct 2026
Free to preview. The first 3 cards and the full outline are below — the rest of this sheet is part of the DSA & Data Structures bundle.

What's inside

Preview — first 3 of 118 cards

What is backtracking?

A systematic way to build solutions incrementally and abandon (backtrack from) a partial solution as soon as it can't be completed. It is DFS over an implicit state-space tree: nodes are partial solutions, edges are choices, and only the current root-to-node path is held in memory.

def subsets(nums):
    res, path = [], []
    def bt(i):
        if i == len(nums):
            res.append(path[:])   # snapshot, never the live list
            return
        bt(i + 1)                  # exclude nums[i]
        path.append(nums[i])       # choose
        bt(i + 1)                  # explore
        path.pop()                 # unchoose
    bt(0)
    return res

Backtracking vs brute force

Brute force enumerates all possibilities blindly. Backtracking prunes branches that violate constraints early, often exponentially faster in practice.

Decision tree

Each node is a partial solution and each edge is a choice. Leaves are complete (possibly invalid) solutions. Backtracking traverses depth-first, pruning invalid subtrees.

115 more cards in this sheet

Unlock the DSA & Data Structures bundle — ₹399

One-time payment. Unlocks this sheet and 8 other sheets in the bundle, plus a typeset PDF of each.

Unlock the bundle See all bundles

Or get all 76 sheets for ₹999 — one payment, every future sheet included.