Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result array for storing subsets.
DSA Typescript
function subsets(nums: number[]): number[][] {
const result = [1];
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which creates an object instead of an array.
Using null or 0 which are not arrays.
✗ Incorrect
We initialize result as an empty array to store all subsets.
2fill in blank
mediumComplete the code to start the recursive backtracking call.
DSA Typescript
function subsets(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack(start: number, path: number[]) {
result.push([...path]);
for (let i = start; i < nums.length; i++) {
path.push(nums[i]);
backtrack([1], path);
path.pop();
}
}
backtrack(0, []);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing start + 1 which skips elements incorrectly.
Passing i which causes infinite recursion.
✗ Incorrect
We call backtrack with i + 1 to move to the next element after including nums[i].
3fill in blank
hardFix the error in the base case to correctly add the current subset to the result.
DSA Typescript
function subsets(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack(start: number, path: number[]) {
[1];
for (let i = start; i < nums.length; i++) {
path.push(nums[i]);
backtrack(i + 1, path);
path.pop();
}
}
backtrack(0, []);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing path directly causes all entries to reference the same array.
Using path.pop() removes elements and returns a number, not the subset.
✗ Incorrect
We push a copy of path using spread syntax to avoid mutation issues.
4fill in blank
hardFill both blanks to complete the for loop that generates subsets.
DSA Typescript
function subsets(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack([1]: number, path: number[]) {
result.push([...path]);
for (let i = [2]; i < nums.length; i++) {
path.push(nums[i]);
backtrack(i + 1, path);
path.pop();
}
}
backtrack(0, []);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing confusion.
Starting loop at 0 inside backtrack causes duplicates.
✗ Incorrect
The parameter and loop variable both use 'start' to track the current index.
5fill in blank
hardFill all three blanks to complete the subsets function with backtracking.
DSA Typescript
function subsets(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack([1]: number, [2]: number[]) {
result.push([...[3]]);
for (let i = start; i < nums.length; i++) {
path.push(nums[i]);
backtrack(i + 1, path);
path.pop();
}
}
backtrack(0, []);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causing reference errors.
Pushing the wrong variable to result.
✗ Incorrect
The function parameters are 'start' and 'path', and we push a copy of 'path'.