Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the backtracking with an empty combination.
DSA Typescript
function combinationSumK(k: number, n: number): number[][] {
const result: number[][] = [];
function backtrack(start: number, comb: number[], sum: number) {
// backtracking logic here
}
backtrack([1], [], 0);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 instead of 1 causes invalid combinations.
Starting from k or n misses smaller numbers.
✗ Incorrect
We start from number 1 because the problem uses numbers 1 to 9 for combinations.
2fill in blank
mediumComplete the condition to stop backtracking when the combination size reaches k.
DSA Typescript
function backtrack(start: number, comb: number[], sum: number) {
if (comb.length === [1]) {
if (sum === n) {
result.push([...comb]);
}
return;
}
// continue backtracking
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing length to n instead of k.
Using sum instead of length for stopping condition.
✗ Incorrect
We stop when the combination length equals k, the required size.
3fill in blank
hardFix the error in the for-loop range to avoid invalid numbers.
DSA Typescript
for (let i = start; i <= [1]; i++) { if (sum + i > n) break; comb.push(i); backtrack(i + 1, comb, sum + i); comb.pop(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Looping up to n or k causes out-of-range numbers.
Using sum as loop limit causes errors.
✗ Incorrect
Numbers 1 to 9 are allowed, so loop must go up to 9.
4fill in blank
hardFill both blanks to correctly add and remove numbers during backtracking.
DSA Typescript
comb.[1](i); backtrack(i + 1, comb, sum + i); comb.[2]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using shift or unshift changes the wrong end of the array.
Not removing elements causes incorrect combinations.
✗ Incorrect
Use push to add and pop to remove the last element for backtracking.
5fill in blank
hardFill all three blanks to complete the backtracking function signature and call.
DSA Typescript
function backtrack([1]: number, [2]: number[], [3]: number) { // backtracking logic } backtrack(1, [], 0);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names causes confusion.
Mixing types or order breaks the function.
✗ Incorrect
The function uses start (number), comb (array), and sum (number) as parameters.