0
0
DSA Typescriptprogramming~10 mins

Backtracking Concept and Decision Tree Visualization 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 backtracking by calling the helper function.

DSA Typescript
function backtrack(path: number[], nums: number[]): void {
  // base case and recursion logic here
}

function solve(nums: number[]): void {
  backtrack([1], nums);
}
Drag options to blanks, or click blank then click option'
A[1]
B[]
C[nums]
D[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Starting with a non-empty path like [0] or [1] which is incorrect.
Passing the whole nums array as path.
2fill in blank
medium

Complete the code to add the current number to the path inside backtracking.

DSA Typescript
function backtrack(path: number[], nums: number[]): void {
  for (let i = 0; i < nums.length; i++) {
    path.push(nums[i]);
    // recursive call
    backtrack(path, nums);
    path.[1]();
  }
}
Drag options to blanks, or click blank then click option'
Apop
Bpush
Cshift
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() which adds instead of removes.
Using shift() or unshift() which remove or add elements at the start.
3fill in blank
hard

Fix the error in the base case condition to stop recursion when path length equals nums length.

DSA Typescript
function backtrack(path: number[], nums: number[]): void {
  if (path.length [1] nums.length) {
    console.log(path);
    return;
  }
  // recursion continues
}
Drag options to blanks, or click blank then click option'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > which do not correctly detect completion.
Using != which triggers at wrong times.
4fill in blank
hard

Fill both blanks to create a decision tree node with current path and index.

DSA Typescript
interface TreeNode {
  path: number[];
  index: number;
}

function createNode(path: number[], idx: number): TreeNode {
  return {
    path: path[1],
    index: [2]
  };
}
Drag options to blanks, or click blank then click option'
A.slice()
B.push()
Cidx
Dpath.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() which modifies the original array.
Using path.length instead of idx for index.
5fill in blank
hard

Fill all three blanks to build a map of paths to their next choices in the decision tree.

DSA Typescript
function buildDecisionTree(nums: number[]): Map<string, number[]> {
  const tree = new Map<string, number[]>();
  function backtrack(path: number[]): void {
    const key = path.[1]();
    tree.set(key, []);
    for (let num of nums) {
      if (!path.includes(num)) {
        tree.get(key)!.[2](num);
        backtrack(path.[3](num));
      }
    }
  }
  backtrack([]);
  return tree;
}
Drag options to blanks, or click blank then click option'
Ajoin(',')
Bpush
Cconcat
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes elements instead of adding.
Using push() on path array instead of map array.
Using join without separator causing ambiguous keys.