0
0
DSA Typescriptprogramming~10 mins

N Queens Problem in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - N Queens Problem
Start with empty board
Place queen in row 0, col 0
Check if safe
Place queen
Move to next row
Repeat placement
If all rows placed
Solution found
Backtrack if no safe col
Try next col in previous row
Repeat until all solutions found or all options exhausted
The flow tries to place queens row by row, checking safety, placing if safe, moving to next row, or backtracking if no safe position.
Execution Sample
DSA Typescript
function solveNQueens(row) {
  if (row === N) {
    printBoard();
    return;
  }
  for (let col = 0; col < N; col++) {
    if (isSafe(row, col)) {
      placeQueen(row, col);
      solveNQueens(row + 1);
      removeQueen(row, col);
    }
  }
}
This code tries to place queens row by row, checking safety, placing, recursing, and backtracking.
Execution Table
StepOperationRowColActionBoard StatePointer ChangesVisual State
1Try place queen00Check safeEmpty boardNone[. . . .] [. . . .] [. . . .] [. . . .]
2Place queen00Place QQueen at (0,0)queenPositions[0]=0[Q . . .] [. . . .] [. . . .] [. . . .]
3Try place queen10Check safeQueen at (0,0)None[Q . . .] [. . . .] [. . . .] [. . . .]
4Try place queen11Check safeQueen at (0,0)None[Q . . .] [. . . .] [. . . .] [. . . .]
5Place queen11Place QQueens at (0,0),(1,1)queenPositions[1]=1[Q . . .] [. Q . .] [. . . .] [. . . .]
6Try place queen20Check safeQueens at (0,0),(1,1)None[Q . . .] [. Q . .] [. . . .] [. . . .]
7Try place queen21Check safeQueens at (0,0),(1,1)None[Q . . .] [. Q . .] [. . . .] [. . . .]
8Try place queen22Check safeQueens at (0,0),(1,1)None[Q . . .] [. Q . .] [. . . .] [. . . .]
9Place queen22Place QQueens at (0,0),(1,1),(2,2)queenPositions[2]=2[Q . . .] [. Q . .] [. . Q .] [. . . .]
10Try place queen30Check safeQueens at (0,0),(1,1),(2,2)None[Q . . .] [. Q . .] [. . Q .] [. . . .]
11Try place queen31Check safeQueens at (0,0),(1,1),(2,2)None[Q . . .] [. Q . .] [. . Q .] [. . . .]
12Try place queen32Check safeQueens at (0,0),(1,1),(2,2)None[Q . . .] [. Q . .] [. . Q .] [. . . .]
13Try place queen33Check safeQueens at (0,0),(1,1),(2,2)None[Q . . .] [. Q . .] [. . Q .] [. . . .]
14Backtrack33Remove Q at (2,2)Queens at (0,0),(1,1)queenPositions[2]=-1[Q . . .] [. Q . .] [. . . .] [. . . .]
15Backtrack22Remove Q at (1,1)Queen at (0,0)queenPositions[1]=-1[Q . . .] [. . . .] [. . . .] [. . . .]
16Try place queen12Check safeQueen at (0,0)None[Q . . .] [. . . .] [. . . .] [. . . .]
17Place queen12Place QQueens at (0,0),(1,2)queenPositions[1]=2[Q . . .] [. . Q .] [. . . .] [. . . .]
18Try place queen20Check safeQueens at (0,0),(1,2)None[Q . . .] [. . Q .] [. . . .] [. . . .]
19Place queen20Place QQueens at (0,0),(1,2),(2,0)queenPositions[2]=0[Q . . .] [. . Q .] [Q . . .] [. . . .]
20Try place queen30Check safeQueens at (0,0),(1,2),(2,0)None[Q . . .] [. . Q .] [Q . . .] [. . . .]
21Try place queen31Check safeQueens at (0,0),(1,2),(2,0)None[Q . . .] [. . Q .] [Q . . .] [. . . .]
22Place queen31Place QQueens at (0,0),(1,2),(2,0),(3,1)queenPositions[3]=1[Q . . .] [. . Q .] [Q . . .] [. Q . .]
23Solution found--All queens placedFull boardqueenPositions=[0,2,0,1][Q . . .] [. . Q .] [Q . . .] [. Q . .]
💡 All rows processed or backtracked fully, solutions found or no more options
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 9After Step 14After Step 17After Step 19After Step 22Final
queenPositions[-1,-1,-1,-1][0,-1,-1,-1][0,1,-1,-1][0,1,2,-1][0,1,-1,-1][0,2,-1,-1][0,2,0,-1][0,2,0,1][0,2,0,1]
row011221234
Key Moments - 3 Insights
Why do we remove a queen after trying all columns in a row?
Because if no safe position is found in the next rows, we must backtrack and try a different position in the previous row. See steps 13-15 where queen at (2,2) is removed to try other options.
How do we know if a position is safe to place a queen?
We check if no other queen is in the same column or diagonal. This is done before placing a queen, as shown in steps 1, 3, 4, 6, etc., where 'Check safe' is performed.
Why does the algorithm stop when row equals N?
Because placing queens in all rows means a solution is found. Step 23 shows 'Solution found' when row 4 (N=4) is reached.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the queenPositions array after step 5?
A[0,1,-1,-1]
B[0,0,-1,-1]
C[-1,-1,-1,-1]
D[1,1,-1,-1]
💡 Hint
Check the queenPositions variable in variable_tracker after step 5
At which step does the algorithm backtrack from placing queen at (2,2)?
AStep 17
BStep 9
CStep 14
DStep 22
💡 Hint
Look for 'Backtrack' and 'Remove Q at (2,2)' in execution_table rows
If we change N to 3, what will happen to the solution found step?
AIt will occur earlier
BIt will never occur
CIt will occur at step 23
DIt will occur multiple times
💡 Hint
N=3 has no solutions, so 'Solution found' step won't appear in execution_table
Concept Snapshot
N Queens Problem:
- Place N queens on NxN board
- No two queens attack each other
- Use backtracking: place queen row by row
- Check safety before placing
- Backtrack if stuck
- Find all solutions by exploring all options
Full Transcript
The N Queens Problem places N queens on an NxN chessboard so that no two queens attack each other. The algorithm tries to place a queen in each row, checking if the position is safe by ensuring no other queen is in the same column or diagonal. If safe, it places the queen and moves to the next row. If no safe position is found in a row, it backtracks by removing the queen from the previous row and tries the next column. This continues until all queens are placed, which means a solution is found. The execution table shows step-by-step how queens are placed, checked, and removed, with the board state updated visually. The variable tracker shows how queen positions change after each step. Key moments clarify why backtracking is needed, how safety is checked, and when the algorithm stops. The visual quiz tests understanding of queen positions, backtracking steps, and solution existence for different board sizes.