Backtracking

Explore all possibilities recursively and prune invalid branches early.

0 Problems3-4 hours

Overview

Backtracking is a recursive technique that builds solutions incrementally and abandons ("backtracks") partial solutions as soon as they cannot lead to a valid answer. It is used for permutations, combinations, subsets, and constraint-satisfaction problems.

Complexity Summary

Time Complexity

Exponential in general O(2ⁿ) or O(n!) depending on problem

Space Complexity

O(n) recursion depth

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

Choose

Concept

pick an element to add to the current path

Practice questions for this pattern
2

Explore

Concept

recursively build on the choice

Practice questions for this pattern
3

Unchoose

Concept

remove the element and try the next option

Practice questions for this pattern
4

Include/Exclude

Concept

binary choice at each element (subsets, knapsack)

Practice questions for this pattern
5

Pruning

Concept

skip invalid choices early to reduce search space

Practice questions for this pattern
6

Avoid duplicates

Concept

sort input and skip duplicate elements at the same level

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview