Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the permutation generation by calling the helper function.
DSA Typescript
function permute(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack(start: number) {
// helper function body
}
[1];
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling backtrack with the length of the array instead of 0.
Calling backtrack without any argument.
Starting from a negative index.
✗ Incorrect
We start generating permutations from index 0, so calling backtrack(0) initiates the process correctly.
2fill in blank
mediumComplete the code to swap elements at two indices in the array.
DSA Typescript
function swap(nums: number[], i: number, j: number): void {
const temp = nums[i];
nums[i] = nums[[1]];
nums[[1]] = temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j for the second index.
Using a fixed index like 0 or nums.length - 1.
✗ Incorrect
To swap elements at indices i and j, we assign nums[i] = nums[j] and nums[j] = temp.
3fill in blank
hardFix the error in the base case condition to add a copy of the current permutation to the result.
DSA Typescript
function permute(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack(start: number) {
if (start [1] nums.length) {
result.push([...nums]);
return;
}
// rest of code
}
backtrack(0);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or greater than instead of equality.
Using less than or equal which triggers too early.
✗ Incorrect
The base case is when start equals nums.length, meaning a full permutation is formed.
4fill in blank
hardFill both blanks to complete the for loop and swap calls inside the backtrack function.
DSA Typescript
function backtrack(start: number) {
if (start === nums.length) {
result.push([...nums]);
return;
}
for (let i = [1]; i < nums.length; i++) {
swap(nums, start, i);
backtrack([2]);
swap(nums, start, i);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 instead of start.
Calling backtrack with i instead of start + 1.
✗ Incorrect
The loop starts from 'start' index, and after swapping, we recurse with start + 1 to fix the next position.
5fill in blank
hardFill all three blanks to complete the full permutation function with swap helper.
DSA Typescript
function permute(nums: number[]): number[][] {
const result: number[][] = [];
function swap(arr: number[], i: number, j: number): void {
const temp = arr[i];
arr[i] = arr[[1]];
arr[[2]] = temp;
}
function backtrack(start: number) {
if (start === nums.length) {
result.push([...nums]);
return;
}
for (let i = [3]; i < nums.length; i++) {
swap(nums, start, i);
backtrack(start + 1);
swap(nums, start, i);
}
}
backtrack(0);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up i and j in swap assignments.
Starting loop from 0 instead of start.
✗ Incorrect
In swap, we assign arr[i] = arr[j] and arr[j] = temp. The loop in backtrack starts from 'start' index.