Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start backtracking by calling the helper function.
DSA Typescript
function backtrack(path: number[], nums: number[]): void {
// base case and recursion logic here
}
function solve(nums: number[]): void {
backtrack([1], nums);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting with a non-empty path like [0] or [1] which is incorrect.
Passing the whole nums array as path.
✗ Incorrect
We start backtracking with an empty path, so we pass an empty array [].
2fill in blank
mediumComplete the code to add the current number to the path inside backtracking.
DSA Typescript
function backtrack(path: number[], nums: number[]): void {
for (let i = 0; i < nums.length; i++) {
path.push(nums[i]);
// recursive call
backtrack(path, nums);
path.[1]();
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() which adds instead of removes.
Using shift() or unshift() which remove or add elements at the start.
✗ Incorrect
After exploring with the current number, we remove it using pop() to backtrack.
3fill in blank
hardFix the error in the base case condition to stop recursion when path length equals nums length.
DSA Typescript
function backtrack(path: number[], nums: number[]): void {
if (path.length [1] nums.length) {
console.log(path);
return;
}
// recursion continues
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > which do not correctly detect completion.
Using != which triggers at wrong times.
✗ Incorrect
We stop when path length equals nums length, so the condition is path.length == nums.length.
4fill in blank
hardFill both blanks to create a decision tree node with current path and index.
DSA Typescript
interface TreeNode {
path: number[];
index: number;
}
function createNode(path: number[], idx: number): TreeNode {
return {
path: path[1],
index: [2]
};
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() which modifies the original array.
Using path.length instead of idx for index.
✗ Incorrect
We copy the path using slice() to avoid mutation, and set index to idx parameter.
5fill in blank
hardFill all three blanks to build a map of paths to their next choices in the decision tree.
DSA Typescript
function buildDecisionTree(nums: number[]): Map<string, number[]> {
const tree = new Map<string, number[]>();
function backtrack(path: number[]): void {
const key = path.[1]();
tree.set(key, []);
for (let num of nums) {
if (!path.includes(num)) {
tree.get(key)!.[2](num);
backtrack(path.[3](num));
}
}
}
backtrack([]);
return tree;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes elements instead of adding.
Using push() on path array instead of map array.
Using join without separator causing ambiguous keys.
✗ Incorrect
We join path to string key, push next numbers to map array, and concat num to path for recursion.