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 correct subset, but backtracking tries all subsets to find the right one.
Click to reveal answer
intermediate
How does backtracking handle constraints in problems?
Backtracking checks constraints at each step and abandons paths that break rules early, saving time by not exploring invalid options further.
Click to reveal answer
intermediate
What is a key difference between backtracking and brute force?
Backtracking prunes paths that cannot lead to a solution early, while brute force tries all possibilities without early stopping.
Click to reveal answer
Why does greedy fail in some problems?
AIt tries all possible solutions
BIt uses recursion to explore options
CIt always backtracks when stuck
DIt only looks at the current best choice without considering future consequences
✗ Incorrect
Greedy algorithms pick the best immediate option but may miss better overall solutions that require looking ahead.
What does backtracking do when it hits a dead end?
AReturns to the previous step and tries a different option
BChooses the first option again
CStops the entire process
DSkips to the last step
✗ Incorrect
Backtracking goes back one step to try other choices when a path fails.
Which problem is best solved by backtracking rather than greedy?
AFinding the maximum value in an array
BFinding the minimum in a sorted list
CSubset sum problem
DSorting numbers
✗ Incorrect
Subset sum requires exploring combinations, which backtracking does well but greedy cannot guarantee.
How does backtracking improve over brute force?
ABy pruning invalid paths early
BBy trying fewer options randomly
CBy using greedy choices
DBy sorting inputs first
✗ Incorrect
Backtracking stops exploring paths that break constraints early, saving time.
Which of these is NOT a characteristic of greedy algorithms?
AMaking local optimal choices
BBacktracking when stuck
CFast and simple
DMay not find global optimum
✗ Incorrect
Greedy algorithms do not backtrack; they commit to choices immediately.
Explain why backtracking is needed when greedy algorithms fail. Include an example.
Think about problems where looking ahead matters.
You got /4 concepts.
Describe how backtracking explores solutions and avoids unnecessary work.
Focus on the process of exploring and undoing choices.
You got /4 concepts.
Practice
(1/5)
1. Why do we need backtracking algorithms instead of just using greedy algorithms for some problems?
easy
A. Because greedy algorithms make local optimal choices that may not lead to a global optimum.
B. Because backtracking algorithms always run faster than greedy algorithms.
C. Because greedy algorithms require more memory than backtracking.
D. Because backtracking algorithms never use recursion.
Solution
Step 1: Understand why backtracking is needed over greedy algorithms
Greedy algorithms pick the best choice at each step without reconsidering previous choices. Backtracking tries all possibilities by undoing choices, ensuring global optimum is found when greedy fails.
Final Answer:
Because greedy algorithms make local optimal choices that may not lead to a global optimum. -> Option A
Quick Check:
Greedy local vs backtracking global = A [OK]
Hint: Greedy picks fast local best; backtracking explores all options [OK]
Common Mistakes:
Thinking greedy is always faster and better
Confusing recursion use in backtracking
Assuming greedy always finds global optimum
2. Which of the following is the correct syntax to start a backtracking function in C#?
Hint: C# methods need return type and parentheses [OK]
Common Mistakes:
Using JavaScript or other language syntax
Missing parentheses for parameters
Using array brackets incorrectly
3. Consider this C# backtracking code snippet to find subsets of {1, 2}:
void Backtrack(List<int> subset, int start) {
Console.WriteLine(string.Join(",", subset));
for (int i = start; i < 2; i++) {
subset.Add(i + 1);
Backtrack(subset, i + 1);
subset.RemoveAt(subset.Count - 1);
}
}
Backtrack(new List<int>(), 0);
What is the printed output?
medium
A. "",1,1,2
B. "",1,2,1,2
C. ",1,2,1,2"
D. "",1,1,2,2
Solution
Step 1: Trace initial call, recursion, and backtracking prints
First call prints empty subset "". Then loop adds 1, prints "1", recurses adding 2 prints "1,2". Backtracks, then adds 2 alone, prints "2". The print sequence is "",1,2,1,2.
Final Answer:
"",1,2,1,2 -> Option B
Quick Check:
Print sequence "",1,2,1,2 = C [OK]
Hint: Trace recursion and backtrack steps carefully [OK]
Common Mistakes:
Confusing order of prints
Missing empty subset print
Not removing last element after recursion
4. The following C# code tries to solve the subset sum problem using greedy approach:
int[] nums = {3, 1, 7, 5};
int target = 8;
int sum = 0;
foreach (int num in nums) {
if (sum + num <= target) {
sum += num;
}
}
Console.WriteLine(sum);
What is the problem with this code?
medium
A. It always finds the correct subset sum.
B. It causes a runtime error due to wrong loop syntax.
C. It may not find a subset that sums exactly to the target.
D. It uses backtracking instead of greedy.
Solution
Step 1: Analyze greedy subset sum limitation
The code adds numbers if sum stays below target, without trying all subsets. Greedy may skip combinations that sum exactly to target, so it may fail to find exact subset.
Final Answer:
It may not find a subset that sums exactly to the target. -> Option C
Quick Check:
Greedy misses exact subset sums = A [OK]
Hint: Greedy skips options; subset sum needs backtracking [OK]
Common Mistakes:
Assuming greedy always works for subset sum
Thinking code has syntax errors
Confusing greedy with backtracking
5. You want to find all ways to place 3 queens on a 3x3 chessboard so that no two queens attack each other. Why is backtracking necessary and greedy insufficient for this problem?
hard
A. Because greedy always finds all solutions faster than backtracking.
B. Because greedy can only solve problems with numeric inputs.
C. Because backtracking uses less memory than greedy in this problem.
D. Because greedy places queens without checking future conflicts, missing valid solutions.
Solution
Step 1: Understand greedy limits and backtracking advantage for N-Queens
Greedy places queens step-by-step without revisiting choices, may cause conflicts later. Backtracking tries placing queens, and if conflict arises, removes and tries alternatives, ensuring all valid solutions.
Final Answer:
Because greedy places queens without checking future conflicts, missing valid solutions. -> Option D
Quick Check:
Backtracking explores all; greedy misses conflicts = B [OK]
Hint: Backtracking tries all; greedy stops early [OK]
Common Mistakes:
Thinking greedy finds all solutions
Confusing memory use between methods
Assuming greedy only works with numbers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.