DSA interview question
A chess-puzzle app wants to show players every way to place n queens on an n × n board so that no two queens attack each other: no two share a row, a column, or a diagonal. Each row must contain exactly one queen, so the search places queens row by row. A column is allowed only if no earlier queen covers its column, its "" diagonal (constant row − col) or its "/" diagonal (constant row + col). Three boolean arrays make each check O(1).
Example: n = 4 → two boards, .Q.. / ...Q / Q... / ..Q. and ..Q. / Q... / ...Q / .Q...
vector<vector<string>> solveNQueens(int n); // each board: n strings of '.' and 'Q'
Input n | Output | Explanation |
|---|---|---|
4 | [".Q..","...Q","Q...","..Q."] ["..Q.","Q...","...Q",".Q.."] | 2 solutions |
1 | ["Q"] | |
2 | (none) | |
3 | (none) | |
8 | 92 solutions | The classic eight-queens puzzle |
(Confirm: 1 ≤ n ≤ 9; any order of boards is acceptable; this solution lists them in column order of the first row.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in