Challenge - 5 Problems
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Backtracking Subset Generation
What is the printed output of the following Python code that uses backtracking to generate subsets of [1, 2]?
DSA Python
def backtrack(start, path): print(path) for i in range(start, len(nums)): path.append(nums[i]) backtrack(i + 1, path) path.pop() nums = [1, 2] backtrack(0, [])
Attempts:
2 left
💡 Hint
Trace the recursive calls and note when the path is printed.
✗ Incorrect
The function prints the current path before exploring further elements. It starts with an empty list, then adds 1, then adds 2, backtracks, then adds 2 alone.
🧠 Conceptual
intermediateBacktracking Decision Tree Nodes Count
How many nodes are there in the decision tree when generating all subsets of a list with 3 elements using backtracking?
Attempts:
2 left
💡 Hint
Each element can be either included or excluded, forming a binary tree.
✗ Incorrect
For 3 elements, the decision tree has 2^3 = 8 leaves and total nodes = 2^(3+1) - 1 = 15.
🔧 Debug
advancedIdentify the Error in Backtracking Code
What error does the following backtracking code produce when run?
DSA Python
def backtrack(path): print(path) for i in range(len(nums)): if nums[i] in path: continue path.append(nums[i]) backtrack(path) path.pop() nums = [1, 2] backtrack([])
Attempts:
2 left
💡 Hint
The uniqueness check prevents infinite recursion; trace to see it generates permutations instead of subsets.
✗ Incorrect
The code runs without error due to the 'if nums[i] in path' check preventing duplicates, but lacks a start index so generates permutations ([] [1] [1,2] [2] [2,1]) instead of subsets.
🚀 Application
advancedBacktracking to Solve N-Queens - Number of Solutions
Using backtracking, how many distinct solutions exist for the 4-Queens problem?
Attempts:
2 left
💡 Hint
Try to visualize placing queens row by row without attacking each other.
✗ Incorrect
The 4-Queens problem has exactly 2 distinct solutions.
🧠 Conceptual
expertBacktracking Tree Pruning Effect
In backtracking, what is the main effect of pruning branches early in the decision tree?
Attempts:
2 left
💡 Hint
Think about how skipping impossible paths affects performance.
✗ Incorrect
Pruning removes unnecessary branches early, reducing the number of recursive calls and speeding up the search.
