Complete One Backtracking Frame
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Practice workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Practice workspace
Problem
Implement expand_backtracking_frame(path, choices). Return one new candidate path per choice by appending that choice to path. Preserve choices order, allocate independent result lists, and leave both inputs unchanged.
Starter code
def expand_backtracking_frame(path, choices):
passTest cases
three-choices
{
"args": [
[
1
],
[
"a",
"b",
"c"
]
]
}Expected: [[1,"a"],[1,"b"],[1,"c"]]
empty-path
{
"args": [
[],
[
2,
3
]
]
}Expected: [[2],[3]]
Wizard outline
- Step 1: Return no candidate frames
Return an empty snapshots list when choices is empty without changing path. The zero-branch case defines both result shape and the no-mutation guarantee.
- Step 2: Snapshot choices from an empty path
Append each choice, copy the candidate, and pop so the next branch begins empty. An empty parent path makes the choose-copy-unchoose lifecycle visible with no inherited state.
- Step 3: Preserve an existing parent path
Run the same balanced mutation frame on any parent path and preserve duplicate branches independently. Removing the teaching guard generalizes the proven lifecycle without changing its state restoration rule.
Footguns and prerequisites
- Appending to one shared path without undo makes later branches contain earlier choices.
- Appending the same mutable path object to results makes all candidates change together.
- recursion and backtracking
Reviewed references
Prepared Interview Problems
- Generate Balanced Parentheses(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing generate parentheses as a complete Interview Problem.
- Maximum Unique String Split(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing max unique split as a complete Interview Problem.
- Palindrome Partitioning(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing palindrome partitioning as a complete Interview Problem.
- Permutations of Distinct Values(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing permutations as a complete Interview Problem.
- Phone Keypad Letter Combinations(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing letter combinations phone as a complete Interview Problem.
- Power Set of Distinct Values(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing subsets as a complete Interview Problem.
- Restore Valid IPv4 Addresses(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing restore ip addresses as a complete Interview Problem.
- Reusable-Candidate Combination Sum(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing combination sum as a complete Interview Problem.
- Single-Use Combination Sum(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing combination sum two as a complete Interview Problem.
- Unique Subsets With Duplicate Inputs(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing subsets two as a complete Interview Problem.
- Word Search II(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing word search two as a complete Interview Problem.
- Word Search in a Character Grid(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing word search as a complete Interview Problem.
Recommended approach and implementation
Each backtracking branch needs a path state that includes one choice but does not leak that choice into sibling branches. Before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element.
Why it works: Appending one choice creates exactly one candidate extension, and copying it prevents later mutations from changing prior output. Popping restores the original path before the next choice, so all and only one-step branches are emitted.
def expand_backtracking_frame(path, choices):
candidates = []
for choice in choices:
path.append(choice)
candidates.append(path.copy())
path.pop()
return candidates