Bird
Raised Fist0
DSA C#programming~10 mins

N Queens Problem in DSA C# - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the chessboard with '.' representing empty cells.

DSA C#
char[][] board = new char[n][];
for (int i = 0; i < n; i++) {
    board[i] = new char[n];
    for (int j = 0; j < n; j++) {
        board[i][j] = [1];
    }
}
Drag options to blanks, or click blank then click option'
A'.'
B'X'
C'O'
D'Q'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Q' instead of '.' will mark all cells as queens initially.
Using 'X' or 'O' are not standard representations for empty cells.
2fill in blank
medium

Complete the code to check if a queen can be placed at board[row][col] safely.

DSA C#
for (int i = 0; i < row; i++) {
    if (board[i][[1]] == 'Q') return false;
}
Drag options to blanks, or click blank then click option'
Arow
Bcol
Ci
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' instead of 'col' will check the wrong column.
Using 'i' will check diagonal incorrectly here.
3fill in blank
hard

Fix the error in the diagonal check to ensure no queen attacks diagonally.

DSA C#
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
    if (board[i][[1]] == 'Q') return false;
}
Drag options to blanks, or click blank then click option'
Aj
Bi
Crow
Dcol
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'j' will check the wrong column index.
Using 'row' or 'col' here is incorrect as they are fixed indices.
4fill in blank
hard

Fill both blanks to place a queen and then remove it during backtracking.

DSA C#
board[row][[1]] = 'Q';
if (SolveNQueens(board, row + 1)) return true;
board[row][[2]] = '.';
Drag options to blanks, or click blank then click option'
Acol
Brow
Ci
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for placing and removing causes incorrect backtracking.
Using 'row' instead of 'col' will cause index errors.
5fill in blank
hard

Fill all three blanks to complete the recursive call and loop for placing queens.

DSA C#
for (int [1] = 0; [2] < board.Length; [3]++) {
    if (IsSafe(board, row, [2])) {
        board[row][[2]] = 'Q';
        if (SolveNQueens(board, row + 1)) return true;
        board[row][[2]] = '.';
    }
}
Drag options to blanks, or click blank then click option'
Acol
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop causes syntax errors.
Using 'row' instead of 'col' will cause logical errors.

Practice

(1/5)
1. What is the main goal of the N Queens Problem in chess?
easy
A. Place N queens on an NxN board so that no two queens attack each other
B. Place N queens on an NxN board so that all queens attack each other
C. Place N queens on an NxN board in a single row
D. Place N queens on an NxN board in a single column

Solution

  1. Step 1: Understand the problem statement

    The N Queens Problem requires placing N queens on an NxN chessboard so that no two queens threaten each other.
  2. Step 2: Identify the correct goal

    This means no two queens share the same row, column, or diagonal.
  3. Final Answer:

    Place N queens on an NxN board so that no two queens attack each other -> Option A
  4. Quick Check:

    Goal = No two queens attack each other [OK]
Hint: Remember: Queens must not threaten each other [OK]
Common Mistakes:
  • Thinking queens can share rows or columns
  • Ignoring diagonal attacks
  • Confusing with placing queens in one line
2. Which of the following C# code snippets correctly declares a 2D array to represent an 8x8 chessboard for the N Queens Problem?
easy
A. int[,] board = new int[8,8];
B. int[][] board = new int[8];
C. int board[8][8];
D. int board = new int[8,8];

Solution

  1. Step 1: Identify correct 2D array declaration in C#

    In C#, a 2D array is declared as int[,] board = new int[8,8];.
  2. Step 2: Check other options for syntax errors

    int[][] board = new int[8]; declares a jagged array but misses initialization of inner arrays. int board[8][8]; is invalid syntax in C#. int board = new int[8,8]; tries to assign an array to an int variable.
  3. Final Answer:

    int[,] board = new int[8,8]; -> Option A
  4. Quick Check:

    Correct 2D array syntax = int[,] board = new int[8,8]; [OK]
Hint: Use int[,] for 2D arrays in C# [OK]
Common Mistakes:
  • Using jagged arrays without initializing inner arrays
  • Using C++ style array declarations
  • Assigning arrays to single int variables
3. Consider the following C# code snippet for checking if a queen can be placed at row 1, column 2 on a 4x4 board:
bool IsSafe(int[,] board, int row, int col) {
    for (int i = 0; i < col; i++) {
        if (board[row, i] == 1) return false;
    }
    for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
        if (board[i, j] == 1) return false;
    }
    for (int i = row, j = col; i < 4 && j >= 0; i++, j--) {
        if (board[i, j] == 1) return false;
    }
    return true;
}

int[,] board = new int[4,4];
board[0, 1] = 1;
bool result = IsSafe(board, 1, 2);
Console.WriteLine(result);
What will be the output?
medium
A. true
B. Compile-time error
C. false
D. Runtime exception

Solution

  1. Step 1: Analyze the board state and IsSafe checks

    The board has a queen at (0,1). We check if placing at (1,2) is safe. The function checks left row, upper-left diagonal, and lower-left diagonal.
  2. Step 2: Check conflicts for position (1,2)

    Left row check: no queen at (1,0) or (1,1). Upper-left diagonal: (0,1) has a queen, so returns false.
  3. Final Answer:

    false -> Option C
  4. Quick Check:

    Queen at (0,1) blocks (1,2) = false [OK]
Hint: Check diagonals carefully for conflicts [OK]
Common Mistakes:
  • Ignoring diagonal checks
  • Assuming no conflict if row is clear
  • Misreading array indices
4. The following C# code attempts to solve the N Queens Problem but has a bug. What is the bug?
bool SolveNQueens(int[,] board, int col) {
    if (col >= board.GetLength(0)) return true;
    for (int i = 0; i < board.GetLength(0); i++) {
        if (IsSafe(board, i, col)) {
            board[i, col] = 1;
            if (SolveNQueens(board, col + 1)) return true;
            board[i, col] = 0;
        }
    }
    return false;
}

// IsSafe method is correct
medium
A. The function does not backtrack properly
B. No bug; code works correctly
C. The board array is not initialized
D. The base case should check row instead of column

Solution

  1. Step 1: Review the recursive backtracking logic

    The function tries to place a queen in each row of the current column, checks safety, places queen, and recurses to next column.
  2. Step 2: Verify base case and backtracking

    Base case returns true when all columns are processed. Backtracking resets board[i, col] to 0 if recursion fails. This is correct.
  3. Final Answer:

    No bug; code works correctly -> Option B
  4. Quick Check:

    Backtracking logic and base case are correct [OK]
Hint: Check base case and backtracking steps carefully [OK]
Common Mistakes:
  • Confusing row and column in base case
  • Forgetting to reset board on backtrack
  • Assuming uninitialized board causes error
5. You want to modify the N Queens solver to count all possible solutions for N=4 instead of stopping at the first. Which change should you make to the recursive method?
hard
A. Add a global boolean flag to stop after first solution
B. Remove the IsSafe check to speed up counting
C. Change the board size to 5x5
D. Replace the return type with int and sum all recursive calls instead of returning on first true

Solution

  1. Step 1: Understand current behavior

    The current solver returns true on finding the first solution, stopping further search.
  2. Step 2: Modify to count all solutions

    Change return type to int, accumulate counts from recursive calls, and do not return early. This counts all valid placements.
  3. Final Answer:

    Replace the return type with int and sum all recursive calls instead of returning on first true -> Option D
  4. Quick Check:

    Counting solutions = sum recursive counts [OK]
Hint: Sum all recursive results to count solutions [OK]
Common Mistakes:
  • Stopping at first solution
  • Changing board size unnecessarily
  • Removing safety checks causing wrong counts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard