Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the backtracking with an empty combination.
DSA C#
List<IList<int>> result = new List<IList<int>>(); Backtrack(new List<int>(), 1, [1], result); void Backtrack(List<int> combination, int start, int target, List<IList<int>> result) { // implementation }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting backtracking with 0 instead of k.
Using n instead of k as the target.
✗ Incorrect
We start backtracking with the target sum k to find combinations that sum to k.
2fill in blank
mediumComplete the condition to stop backtracking when the target is reached.
DSA C#
void Backtrack(List<int> combination, int start, int target, List<IList<int>> result) {
if ([1] == 0) {
result.Add(new List<int>(combination));
return;
}
// continue backtracking
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking combination.Count instead of target.
Checking start or result.Count incorrectly.
✗ Incorrect
When target reaches zero, the current combination sums to k and should be added to results.
3fill in blank
hardFix the error in the loop to avoid exceeding the target sum.
DSA C#
for (int i = start; i <= 9; i++) { if (i > [1]) break; combination.Add(i); Backtrack(combination, i + 1, target - i, result); combination.RemoveAt(combination.Count - 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing i with start or combination.Count instead of target.
Not breaking the loop causing unnecessary recursion.
✗ Incorrect
We break the loop if the current number i exceeds the remaining target to avoid invalid sums.
4fill in blank
hardFill both blanks to correctly update the combination and recurse.
DSA C#
combination.[1](i); Backtrack(combination, i [2] 1, target - i, result); combination.RemoveAt(combination.Count - 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Insert instead of Add.
Using subtraction instead of addition for start.
✗ Incorrect
Add i to combination, then recurse with start as i + 1 to avoid duplicates.
5fill in blank
hardFill both blanks to complete the LINQ expression for filtering combinations.
DSA C#
var filtered = combinations.Where(c => c.Sum() [1] [2]).ToList(); // Print filtered combinations foreach (var combo in filtered) { Console.WriteLine(string.Join(", ", combo)); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < instead of == for exact sum match.
Comparing with wrong variable.
✗ Incorrect
Filter combinations whose sum equals k using == operator and k value.
