Episode 7 — DSA with JavaScript / 7.15 — Recursion and Backtracking

7.15 — Quick Revision: Recursion & Backtracking

<< Overview


Key concepts

Backtracking template

function backtrack(state):
    if goal reached → add to results
    for each choice:
        if valid:
            make choice
            backtrack(next state)
            undo choice  ← KEY STEP

Common problems

ProblemComplexityKey technique
SubsetsO(2ⁿ)Include/exclude
PermutationsO(n!)Swap or remaining
N-QueensO(n!)Row-by-row, column check
SudokuO(9^empty)Try digits, validate
Combination SumO(2ⁿ)Track remaining

Self-check drill

SC-001

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-002

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-003

  • Q: Subset generation?
  • A: Include/exclude each element

SC-004

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-005

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-006

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-007

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-008

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-009

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-010

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-011

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-012

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-013

  • Q: Subset generation?
  • A: Include/exclude each element

SC-014

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-015

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-016

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-017

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-018

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-019

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-020

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-021

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-022

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-023

  • Q: Subset generation?
  • A: Include/exclude each element

SC-024

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-025

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-026

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-027

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-028

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-029

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-030

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-031

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-032

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-033

  • Q: Subset generation?
  • A: Include/exclude each element

SC-034

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-035

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-036

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-037

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-038

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-039

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-040

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-041

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-042

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-043

  • Q: Subset generation?
  • A: Include/exclude each element

SC-044

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-045

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-046

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-047

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-048

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-049

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-050

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-051

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-052

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-053

  • Q: Subset generation?
  • A: Include/exclude each element

SC-054

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-055

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-056

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-057

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-058

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-059

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-060

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-061

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-062

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-063

  • Q: Subset generation?
  • A: Include/exclude each element

SC-064

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-065

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-066

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-067

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-068

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-069

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-070

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-071

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-072

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-073

  • Q: Subset generation?
  • A: Include/exclude each element

SC-074

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-075

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-076

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-077

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-078

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-079

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-080

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-081

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-082

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-083

  • Q: Subset generation?
  • A: Include/exclude each element

SC-084

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-085

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-086

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-087

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-088

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-089

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-090

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-091

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-092

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-093

  • Q: Subset generation?
  • A: Include/exclude each element

SC-094

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-095

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-096

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-097

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-098

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-099

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-100

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-101

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-102

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-103

  • Q: Subset generation?
  • A: Include/exclude each element

SC-104

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-105

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-106

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-107

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-108

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-109

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-110

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-111

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-112

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-113

  • Q: Subset generation?
  • A: Include/exclude each element

SC-114

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-115

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-116

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-117

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-118

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-119

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-120

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-121

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-122

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-123

  • Q: Subset generation?
  • A: Include/exclude each element

SC-124

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-125

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-126

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-127

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-128

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-129

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-130

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-131

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-132

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-133

  • Q: Subset generation?
  • A: Include/exclude each element

SC-134

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-135

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-136

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-137

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-138

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-139

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-140

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-141

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-142

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-143

  • Q: Subset generation?
  • A: Include/exclude each element

SC-144

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-145

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-146

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-147

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-148

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-149

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-150

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-151

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-152

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-153

  • Q: Subset generation?
  • A: Include/exclude each element

SC-154

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-155

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-156

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-157

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-158

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-159

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-160

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-161

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-162

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-163

  • Q: Subset generation?
  • A: Include/exclude each element

SC-164

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-165

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-166

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-167

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-168

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-169

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-170

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-171

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-172

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-173

  • Q: Subset generation?
  • A: Include/exclude each element

SC-174

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-175

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-176

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-177

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-178

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-179

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-180

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-181

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-182

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-183

  • Q: Subset generation?
  • A: Include/exclude each element

SC-184

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-185

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-186

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-187

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-188

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-189

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-190

  • Q: Base case importance?
  • A: Terminates recursion, returns result

SC-191

  • Q: Backtracking pattern?
  • A: Choose → Explore → Unchoose

SC-192

  • Q: N-Queens complexity?
  • A: O(n!) with pruning

SC-193

  • Q: Subset generation?
  • A: Include/exclude each element

SC-194

  • Q: Permutations complexity?
  • A: O(n × n!)

SC-195

  • Q: How to avoid duplicates?
  • A: Sort + skip same adjacent

SC-196

  • Q: Pruning?
  • A: Cut branches that can't succeed

SC-197

  • Q: Sudoku solver approach?
  • A: Try 1-9, validate, backtrack

SC-198

  • Q: Word search approach?
  • A: DFS + mark visited + unmark

SC-199

  • Q: Combination sum?
  • A: Backtrack with remaining target

SC-200

  • Q: Base case importance?
  • A: Terminates recursion, returns result