0
0
DSA Typescriptprogramming~20 mins

Why Backtracking and What Greedy Cannot Solve in DSA Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Backtracking Subset Sum
What is the output of the following TypeScript code that uses backtracking to find subsets summing to 5?
DSA Typescript
function backtrack(nums: number[], target: number, start: number, path: number[], res: number[][]): void {
  if (target === 0) {
    res.push([...path]);
    return;
  }
  for (let i = start; i < nums.length; i++) {
    if (nums[i] > target) continue;
    path.push(nums[i]);
    backtrack(nums, target - nums[i], i + 1, path, res);
    path.pop();
  }
}

const nums = [1, 2, 3, 4];
const res: number[][] = [];
backtrack(nums, 5, 0, [], res);
console.log(res);
A[[1,2,3],[1,4],[2,3]]
B[[1,4],[2,3]]
C[[1,4],[2,3],[5]]
D[[1,2,3,4]]
Attempts:
2 left
💡 Hint
Think about which combinations of numbers add exactly to 5 without repetition.
🧠 Conceptual
intermediate
1:30remaining
Why Greedy Fails for Subset Sum
Why does a greedy algorithm fail to solve the subset sum problem correctly in all cases?
ABecause greedy algorithms make local optimal choices that may miss the correct subset sum.
BBecause greedy algorithms always find the global optimum by checking all subsets.
CBecause greedy algorithms use backtracking to explore all possibilities.
DBecause greedy algorithms sort the input and always find the correct subset.
Attempts:
2 left
💡 Hint
Think about how greedy algorithms decide step-by-step without revisiting choices.
Predict Output
advanced
2:30remaining
Backtracking vs Greedy Output Comparison
What is the output of the following TypeScript code comparing greedy and backtracking approaches for target sum 7?
DSA Typescript
function greedy(nums: number[], target: number): number[] {
  const res: number[] = [];
  let sum = 0;
  for (const num of nums) {
    if (sum + num <= target) {
      res.push(num);
      sum += num;
    }
  }
  return res;
}

function backtrack(nums: number[], target: number, start: number, path: number[], res: number[][]): void {
  if (target === 0) {
    res.push([...path]);
    return;
  }
  for (let i = start; i < nums.length; i++) {
    if (nums[i] > target) continue;
    path.push(nums[i]);
    backtrack(nums, target - nums[i], i + 1, path, res);
    path.pop();
  }
}

const nums = [2, 3, 5, 6];
const greedyResult = greedy(nums, 7);
const backtrackResult: number[][] = [];
backtrack(nums, 7, 0, [], backtrackResult);
console.log({greedyResult, backtrackResult});
A{"greedyResult":[2,3],"backtrackResult":[[2,5],[3,4]]}
B{"greedyResult":[2,3,5],"backtrackResult":[[2,5],[3,5]]}
C{"greedyResult":[2,3],"backtrackResult":[[2,5]]}
D}]]4,3[,]5,2[[:"tluseRkcartkcab",]3,2[:"tluseRydeerg"{
Attempts:
2 left
💡 Hint
Check which subsets sum exactly to 7 and what greedy picks.
🔧 Debug
advanced
2:00remaining
Identify the Error in Greedy Algorithm for Coin Change
What error does the following greedy coin change code produce when trying to make change for 6 with coins [1,3,4]?
DSA Typescript
function greedyCoinChange(coins: number[], amount: number): number[] {
  coins.sort((a, b) => b - a);
  const result: number[] = [];
  for (const coin of coins) {
    while (amount >= coin) {
      result.push(coin);
      amount -= coin;
    }
  }
  if (amount !== 0) throw new Error('Cannot make exact change');
  return result;
}

console.log(greedyCoinChange([1,3,4], 6));
AReturns [4,1,1] which sums to 6
BReturns [4,3] which sums to 7
CReturns [3,3] which sums to 6
DThrows 'Cannot make exact change' error
Attempts:
2 left
💡 Hint
Check how the greedy picks coins starting from largest.
🧠 Conceptual
expert
1:30remaining
Why Backtracking is Needed for N-Queens Problem
Why is backtracking necessary to solve the N-Queens problem instead of a greedy approach?
ABecause greedy algorithms can place queens anywhere without checking conflicts.
BBecause backtracking always places queens in the first row only.
CBecause greedy algorithms use recursion to explore all board positions.
DBecause greedy algorithms cannot guarantee placing all queens without conflicts by local choices alone.
Attempts:
2 left
💡 Hint
Think about how placing one queen affects future placements.