0
0
DSA Typescriptprogramming~10 mins

Generate All Combinations Sum K in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
An
B0
Ck
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 instead of 1 causes invalid combinations.
Starting from k or n misses smaller numbers.
2fill in blank
medium

Complete 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'
Ak
Bn
Csum
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing length to n instead of k.
Using sum instead of length for stopping condition.
3fill in blank
hard

Fix 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'
A9
Bk
Cn
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Looping up to n or k causes out-of-range numbers.
Using sum as loop limit causes errors.
4fill in blank
hard

Fill 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'
Apush
Bpop
Cshift
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using shift or unshift changes the wrong end of the array.
Not removing elements causes incorrect combinations.
5fill in blank
hard

Fill 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'
Astart
Bcomb
Csum
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names causes confusion.
Mixing types or order breaks the function.