Backtracking tries all possible options step-by-step and goes back (undoes) when a choice leads to a dead end, ensuring all solutions are explored.
Click to reveal answer
beginner
Why can't greedy algorithms solve all problems?
Greedy algorithms make the best choice at each step without looking ahead, which can miss the overall best solution in problems needing global decisions.
Click to reveal answer
intermediate
Give an example of a problem where greedy fails but backtracking works.
The "Subset Sum" problem: greedy might pick large numbers first and miss a valid subset, but backtracking tries all subsets to find the correct answer.
Click to reveal answer
intermediate
How does backtracking ensure correctness?
By exploring all possible choices and undoing wrong paths, backtracking guarantees finding all valid solutions or the best one if it exists.
Click to reveal answer
beginner
What is a key drawback of backtracking compared to greedy?
Backtracking can be slow because it tries many possibilities, while greedy is faster but may not always find the best solution.
Click to reveal answer
Which approach tries all possible options and backtracks on failure?
ADivide and Conquer
BBacktracking
CGreedy
DDynamic Programming
✗ Incorrect
Backtracking explores all options and undoes choices when they lead to dead ends.
Why might a greedy algorithm fail to find the best solution?
AIt tries all possibilities
BIt stores all solutions
CIt uses recursion
DIt only looks at the current step
✗ Incorrect
Greedy algorithms make decisions based only on the current step without considering future consequences.
Which problem is a classic example where greedy fails but backtracking succeeds?
ASorting numbers
BBinary Search
CSubset Sum
DFinding maximum
✗ Incorrect
Subset Sum requires checking combinations, which greedy cannot do correctly.
What is a main disadvantage of backtracking?
AIt is slow due to many possibilities
BIt is always incorrect
CIt never finds a solution
DIt uses no recursion
✗ Incorrect
Backtracking can be slow because it explores many possible paths.
Which algorithm type is best when you need the absolute best solution and can afford more time?
ABacktracking
BGreedy
CHeuristic
DSorting
✗ Incorrect
Backtracking tries all options to find the best solution, though it may be slower.
Explain why greedy algorithms cannot solve all problems and how backtracking helps.
Think about choosing step-by-step vs exploring all paths.
You got /4 concepts.
Describe a scenario or problem where backtracking is necessary instead of greedy.
Consider problems where partial choices affect future options.
You got /4 concepts.
Practice
(1/5)
1. Why is backtracking preferred over greedy algorithms in problems like the N-Queens puzzle?
easy
A. Because backtracking explores all possible solutions and can backtrack on wrong choices
B. Because greedy algorithms always find the optimal solution quickly
C. Because backtracking uses less memory than greedy algorithms
D. Because greedy algorithms do not use recursion
Solution
Step 1: Understand problem requirements
The N-Queens puzzle requires placing queens so none attack each other, needing exploration of many possibilities.
Step 2: Compare algorithm approaches
Backtracking tries all options and undoes wrong choices, while greedy picks locally best choices that may fail globally.
Final Answer:
Because backtracking explores all possible solutions and can backtrack on wrong choices -> Option A
Quick Check:
Backtracking explores all options = A [OK]
Hint: Backtracking tries all paths; greedy picks local best only [OK]
Common Mistakes:
Thinking greedy always finds the best solution
Confusing memory use between backtracking and greedy
Assuming backtracking is always slower without context
2. Which of the following is the correct syntax to start a backtracking function in C for solving a problem recursively?
easy
A. int backtrack[] = {0};
B. void backtrack(int step) { /* code */ }
C. backtrack() -> void { /* code */ }
D. function backtrack(int step) { /* code */ }
Solution
Step 1: Identify valid C function syntax
In C, functions are declared with return type, name, and parameters inside parentheses.
Step 2: Check each option
A matches C syntax; B is an array declaration; C and D use non-C syntax.
C. Reached end\nReached end\nReached end\nReached end\n
D. No output
Solution
Step 1: Trace recursive calls
backtrack(0,2) calls backtrack(1,2) twice; each backtrack(1,2) calls backtrack(2,2) twice.
Step 2: Count base case hits
backtrack(2,2) prints "Reached end"; it is called 4 times total (2 calls from each of 2 calls at start=1).
Final Answer:
Reached end\nReached end\nReached end\nReached end\n -> Option C
Quick Check:
4 base calls print output = A [OK]
Hint: Count recursive calls reaching base case carefully [OK]
Common Mistakes:
Counting only one base case print
Confusing number of recursive calls
Ignoring duplicate calls in recursion
4. The following C code attempts to solve the subset sum problem using backtracking but has a bug:
void backtrack(int index, int sum, int target, int arr[], int n) {
if (sum == target) {
printf("Found subset\n");
return;
}
if (index == n || sum > target) {
return;
}
backtrack(index + 1, sum + arr[index], target, arr, n);
backtrack(index + 1, sum, target, arr, n);
}
int main() {
int arr[] = {2, 4, 6};
backtrack(0, 0, 6, arr, 3);
return 0;
}
What is the bug in this code?
medium
A. It does not print all subsets that sum to target
B. It does not check if sum exceeds target before recursion
C. It does not handle negative numbers in array
D. It does not stop recursion after finding one subset
Solution
Step 1: Analyze base case for sum == target
When sum equals target, it prints but continues recursion because no stop condition after printing.
Step 2: Identify missing stop after success
Function returns only from current call, but parent calls continue exploring other subsets.
Final Answer:
It does not stop recursion after finding one subset -> Option D
Quick Check:
Missing stop after success = B [OK]
Hint: Stop recursion immediately after finding a valid subset [OK]
Common Mistakes:
Assuming printing once stops all recursion
Ignoring need to return from all recursive calls
Confusing sum checks with recursion control
5. You want to solve the problem of finding all combinations of coins that sum to a target amount. Why can't a greedy algorithm always solve this correctly, and how does backtracking help?
Choose the best explanation.
hard
A. Greedy picks largest coins first which may miss combinations; backtracking tries all coin choices systematically
B. Greedy uses more memory; backtracking uses less memory
C. Greedy is slower than backtracking for coin problems
D. Backtracking only tries one coin at a time, greedy tries all
Solution
Step 1: Understand greedy approach limitation
Greedy picks largest coin repeatedly, which can miss valid combinations with smaller coins.
Step 2: Understand backtracking advantage
Backtracking tries all coin combinations by exploring all choices and backtracking on failures.
Final Answer:
Greedy picks largest coins first which may miss combinations; backtracking tries all coin choices systematically -> Option A
Quick Check:
Greedy misses combos; backtracking explores all = A [OK]
Hint: Greedy misses combos; backtracking explores all systematically [OK]
Common Mistakes:
Assuming greedy always finds all solutions
Confusing memory use with correctness
Thinking backtracking is always slower without context
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.