0
0
DSA Typescriptprogramming~20 mins

Generate All Combinations Sum K in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Combination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combinations summing to K
What is the output of the following TypeScript code that generates all combinations of numbers from 1 to 9 that sum to 5?
DSA Typescript
function combinationSumK(k: number, n: number): number[][] {
  const result: number[][] = [];
  function backtrack(start: number, target: number, path: number[]) {
    if (target === 0 && path.length === k) {
      result.push([...path]);
      return;
    }
    if (target < 0 || path.length > k) {
      return;
    }
    for (let i = start; i <= 9; i++) {
      path.push(i);
      backtrack(i + 1, target - i, path);
      path.pop();
    }
  }
  backtrack(1, n, []);
  return result;
}

console.log(combinationSumK(2, 5));
A[[1,4],[2,3],[1,2,2]]
B[[1,4],[2,3],[5]]
C[[1,4],[3,2]]
D[[1,4],[2,3]]
Attempts:
2 left
💡 Hint
Focus on combinations of exactly 2 numbers that add up to 5.
Predict Output
intermediate
2:00remaining
Number of combinations for sum and length
How many combinations of 3 numbers from 1 to 9 sum to 9 using the given function?
DSA Typescript
function combinationSumK(k: number, n: number): number[][] {
  const result: number[][] = [];
  function backtrack(start: number, target: number, path: number[]) {
    if (target === 0 && path.length === k) {
      result.push([...path]);
      return;
    }
    if (target < 0 || path.length > k) {
      return;
    }
    for (let i = start; i <= 9; i++) {
      path.push(i);
      backtrack(i + 1, target - i, path);
      path.pop();
    }
  }
  backtrack(1, n, []);
  return result;
}

const combos = combinationSumK(3, 9);
console.log(combos.length);
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
List all 3-number combinations from 1 to 9 that sum to 9.
🔧 Debug
advanced
2:00remaining
Identify the error in combination sum code
What error will this TypeScript code produce when run?
DSA Typescript
function combinationSumK(k: number, n: number): number[][] {
  const result: number[][] = [];
  function backtrack(start: number, target: number, path: number[]) {
    if (target === 0 && path.length === k) {
      result.push(path);
      return;
    }
    if (target < 0 || path.length > k) {
      return;
    }
    for (let i = start; i <= 9; i++) {
      path.push(i);
      backtrack(i + 1, target - i, path);
      path.pop();
    }
  }
  backtrack(1, n, []);
  return result;
}

console.log(combinationSumK(3, 7));
ANo error, outputs correct combinations
BOutput contains references to the same array multiple times
CTypeError: path.push is not a function
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check how arrays are added to the result.
Predict Output
advanced
2:00remaining
Output of combinationSumK with no valid combinations
What is the output of the following code when no combinations of length 4 sum to 3?
DSA Typescript
function combinationSumK(k: number, n: number): number[][] {
  const result: number[][] = [];
  function backtrack(start: number, target: number, path: number[]) {
    if (target === 0 && path.length === k) {
      result.push([...path]);
      return;
    }
    if (target < 0 || path.length > k) {
      return;
    }
    for (let i = start; i <= 9; i++) {
      path.push(i);
      backtrack(i + 1, target - i, path);
      path.pop();
    }
  }
  backtrack(1, n, []);
  return result;
}

console.log(combinationSumK(4, 3));
A[]
B[[]]
C[[1,1,1,0]]
Dnull
Attempts:
2 left
💡 Hint
Can 4 positive distinct numbers sum to 3?
🚀 Application
expert
3:00remaining
Find all combinations of length 3 summing to 9
Using the combinationSumK function, which of the following is the complete set of combinations of length 3 that sum to 9?
DSA Typescript
function combinationSumK(k: number, n: number): number[][] {
  const result: number[][] = [];
  function backtrack(start: number, target: number, path: number[]) {
    if (target === 0 && path.length === k) {
      result.push([...path]);
      return;
    }
    if (target < 0 || path.length > k) {
      return;
    }
    for (let i = start; i <= 9; i++) {
      path.push(i);
      backtrack(i + 1, target - i, path);
      path.pop();
    }
  }
  backtrack(1, n, []);
  return result;
}

console.log(combinationSumK(3, 9));
A[[1,2,6],[1,3,5],[2,3,4]]
B[[1,2,6],[1,3,5],[2,3,4],[1,4,4]]
C[[1,2,6],[1,3,5],[2,3,4],[1,2,5]]
D[[1,2,6],[1,3,5],[2,3,4],[3,3,3]]
Attempts:
2 left
💡 Hint
Check that numbers are distinct and combinations are sorted ascending.