0
0
DSA Typescriptprogramming~20 mins

Backtracking Concept and Decision Tree Visualization in DSA Typescript - Practice Problems & Challenges

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 Path Generation
What is the output of the following TypeScript code that uses backtracking to generate all subsets of [1, 2]?
DSA Typescript
function backtrack(start: number, path: number[], result: number[][]) {
  result.push([...path]);
  for (let i = start; i < 2; i++) {
    path.push(i + 1);
    backtrack(i + 1, path, result);
    path.pop();
  }
}
const result: number[][] = [];
backtrack(0, [], result);
console.log(result);
A[[1], [1, 2], [2]]
B[[], [1], [1, 2], [2]]
C[[], [2], [1], [1, 2]]
D[[1, 2], [1], [2], []]
Attempts:
2 left
💡 Hint
Think about how the function adds the current path before exploring further.
🧠 Conceptual
intermediate
1:30remaining
Understanding Decision Tree Nodes in Backtracking
In a backtracking decision tree for generating permutations of [1, 2, 3], how many nodes are there in total including the root and all leaves?
A16
B15
C24
D10
Attempts:
2 left
💡 Hint
Count nodes at each level: root, first choice, second choice, and leaves.
🔧 Debug
advanced
2:00remaining
Identify the Output of Backtracking with Early Pruning
What is the output of this TypeScript code that generates combinations of [1, 2, 3] of size 2 but prunes paths where sum exceeds 3?
DSA Typescript
function backtrack(start: number, path: number[], result: number[][], sum: number) {
  if (path.length === 2) {
    result.push([...path]);
    return;
  }
  for (let i = start; i < 3; i++) {
    if (sum + i + 1 > 3) continue;
    path.push(i + 1);
    backtrack(i + 1, path, result, sum + i + 1);
    path.pop();
  }
}
const result: number[][] = [];
backtrack(0, [], result, 0);
console.log(result);
A[[1, 2], [1, 3], [2, 3]]
B[[1, 2], [2, 3]]
C[[1, 2]]
D[[1, 2], [1, 3]]
Attempts:
2 left
💡 Hint
Check which pairs have sums less than or equal to 3.
🚀 Application
advanced
1:30remaining
Backtracking to Solve N-Queens: Number of Solutions for N=4
Using backtracking, how many distinct solutions exist for the 4-Queens problem?
A0
B4
C1
D2
Attempts:
2 left
💡 Hint
Try placing queens row by row and count valid configurations.
🧠 Conceptual
expert
1:30remaining
Backtracking Decision Tree Depth for Subset Sum Problem
For a set of 5 elements, what is the maximum depth of the backtracking decision tree when solving the subset sum problem?
A5
B10
C6
D4
Attempts:
2 left
💡 Hint
Each element can be either included or excluded, depth corresponds to decisions per element.