Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start backtracking by calling the helper function.
DSA Java
public void solve() {
backtrack([1]);
}
private void backtrack(List<Integer> path) {
// backtracking logic here
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing null causes NullPointerException.
Passing an existing path may cause incorrect results.
✗ Incorrect
We start backtracking with an empty list to build the solution path step-by-step.
2fill in blank
mediumComplete the code to check if the current path is a solution.
DSA Java
private void backtrack(List<Integer> path) {
if (path.size() == [1]) {
System.out.println(path);
return;
}
// continue backtracking
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing path size to 0 always fails.
Using path.get(0) causes errors.
✗ Incorrect
We check if the path length equals the target size to decide if it's a complete solution.
3fill in blank
hardFix the error in the loop to try all candidates for the next step.
DSA Java
for (int i = 0; i < [1]; i++) { if (!path.contains(candidates[i])) { path.add(candidates[i]); backtrack(path); path.remove(path.size() - 1); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using path.size() limits the loop incorrectly.
Using candidates.size() causes compilation error.
✗ Incorrect
We loop over all candidates using candidates.length to cover all options.
4fill in blank
hardFill both blanks to add and remove the last element in backtracking.
DSA Java
path.[1](candidates[i]); backtrack(path); path.[2](path.size() - 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using append or delete causes compilation errors.
Removing by value instead of index breaks backtracking.
✗ Incorrect
We add the candidate to path, recurse, then remove it to backtrack.
5fill in blank
hardFill all three blanks to build a decision tree node with children.
DSA Java
class TreeNode { int val; List<TreeNode> [1] = new ArrayList<>(); TreeNode(int val) { this.val = [2]; } void addChild(TreeNode child) { [3].add(child); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable names causes errors.
Forgetting to initialize the children list.
✗ Incorrect
The list is named children, val is assigned, and children list adds child nodes.
