Hint: Python functions start with def and parentheses [OK]
Common Mistakes:
Using 'function' keyword (not Python)
Using square brackets instead of parentheses
Using arrow syntax (not Python)
3. What will be the output of the following code?
def backtrack(path, choices):
if not choices:
print(path)
return
for i in range(len(choices)):
backtrack(path + [choices[i]], choices[:i] + choices[i+1:])
backtrack([], [1, 2])
medium
A. [2, 1]
B. [1, 2]
C. [1, 2]\n[2, 1]
D. []
Solution
Step 1: Understand recursion and choices
The function picks each choice, adds it to path, and recurses with remaining choices.
Step 2: Trace calls for [1, 2]
First call: path=[], choices=[1,2]. Picks 1, calls with path=[1], choices=[2]. Then picks 2, path=[1,2], choices=[]. Prints [1,2]. Backtracks and picks 2 first, then 1, prints [2,1].
Final Answer:
[1, 2]
[2, 1] -> Option C
Quick Check:
All permutations of [1,2] printed [OK]
Hint: Backtracking prints all permutations of input list [OK]
Common Mistakes:
Printing only one permutation
Not slicing choices correctly
Confusing path and choices lists
4. Find the error in this backtracking code snippet:
def backtrack(path, choices):
if len(choices) == 0:
print(path)
for i in range(len(choices)):
backtrack(path + [choices[i]], choices[:i])
medium
A. Incorrect slicing of choices in recursive call
B. Missing return after printing path
C. Using + operator on lists is invalid
D. No base case to stop recursion
Solution
Step 1: Analyze recursive call slicing
The recursive call uses choices[:i], which only takes elements before i, missing elements after i.
Step 2: Correct slicing for backtracking
It should be choices[:i] + choices[i+1:] to exclude the chosen element but keep others.
Final Answer:
Incorrect slicing of choices in recursive call -> Option A
Quick Check:
Choices slicing must exclude chosen element correctly [OK]
Hint: Slice choices before and after index to exclude chosen element [OK]
Common Mistakes:
Forgetting to exclude chosen element properly
Not returning after base case
Thinking list + list is invalid
5. You want to generate all subsets of the list [1, 2, 3] using backtracking. Which approach correctly represents the decision tree visualization for this problem?
hard
A. At each step, decide to include or exclude the current element, then move to next
B. At each step, swap elements to generate permutations
C. At each step, remove one element permanently and recurse
D. At each step, sort the list and pick the smallest element
Solution
Step 1: Understand subset generation logic
Generating subsets means choosing to include or exclude each element in order.
Step 2: Match decision tree visualization
The decision tree branches at each element: include or exclude, then move forward. This matches At each step, decide to include or exclude the current element, then move to next.
Final Answer:
At each step, decide to include or exclude the current element, then move to next -> Option A
Quick Check:
Subset tree = include/exclude choices per element [OK]
Hint: Subsets = choose include or exclude each element [OK]
Common Mistakes:
Confusing subsets with permutations
Thinking sorting is needed
Removing elements permanently instead of choosing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.