Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

51. N-Queens

Hard70.7% Acceptance
ArrayBacktracking
Asked by:
A
Amazon
Facebook
ProblemSolutions (8)VideosCompanies (8)Notes

Problem Statement

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.

Example 1:

Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above

Example 2:

Input: n = 1
Output: [["Q"]]

Constraints:

  • 1 <= n <= 9
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
F
M
Microsoft
G
Google
B
Bloomberg
+3

Approach

The N-Queens problem asks you to place N queens on an N x N chessboard so that no two queens attack each other. The most effective strategy is backtracking, where you place queens row by row while ensuring that the current placement does not conflict with previous ones.

At each row, try placing a queen in every column and check whether the column, main diagonal, or anti-diagonal is already occupied. Using data structures such as sets or boolean arrays to track used columns and diagonals helps perform these checks efficiently. If a placement is valid, continue recursively to the next row; otherwise, backtrack and try another position.

This systematic exploration prunes invalid configurations early, significantly reducing the search space. While the worst-case search is large, pruning ensures practical performance for typical constraints.

The typical time complexity is around O(N!) due to the branching possibilities, while the extra space mainly comes from recursion and tracking structures.

Complexity

ApproachTime ComplexitySpace Complexity
Backtracking with column and diagonal trackingO(N!)O(N)
Backtracking with bitmask optimizationO(N!)O(N)

Video Solution Available

Abdul Bari

View all video solutions

Solutions (8)

Backtracking with Row-wise Construction

This approach involves using backtracking to place queens row by row, exploring valid positions recursively. Each queen placement is validated based on column and diagonal restrictions. If a valid placement is found, the algorithm progresses to the next row, otherwise it backtracks and attempts alternative placements.

Time Complexity: O(N!), because we are trying N possible positions for each row for N queens.
Space Complexity: O(N^2), due to the storage of the board states and recursion stack.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdbool.h>
4
5bool isValid(int row, int col, int*



Explanation

This C solution uses a backtracking approach to place queens row by row, ensuring each placement is valid by checking current column and diagonal conflicts. The recursive function solve advances to next row if a queen is placed, or backtracks if a conflict arises. Valid solutions are stored in a dynamically resized list of boards.

Bit Manipulation Optimization

This approach optimizes the placement verification using bit manipulation, reducing the auxiliary space by managing columns and diagonals in integer bitmasks. This permits efficient bitwise operations for placing queens and checking potential attacks.

Time Complexity: O(N!), each row considers N placements, simplified by binary check.
Space Complexity: O(N), due to condensed state tracking via differential lists.

PythonJava
1from typing import List
2
3def solveNQueens(

Video Solutions

Watch expert explanations and walkthroughs

6.1 N Queens Problem using Backtracking

Abdul Bari
13:412,318,339 views

Asked By Companies

8 companies
A
Amazon
F
Facebook
M
Microsoft
G
Google
B
Bloomberg
A
Adobe
B
Bytedance
T
TikTok

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Sudoku SolverHard
Combination SumMedium
Combination Sum IIMedium
PermutationsMedium
More similar problems

Related Topics

ArrayBacktracking

Problem Stats

Acceptance Rate70.7%
DifficultyHard
Companies8

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Can N-Queens be optimized further?

Yes, bitmasking can be used to represent columns and diagonals more efficiently. This reduces overhead from data structures and allows faster conflict checks, which is especially helpful for larger board sizes.

Is N-Queens asked in FAANG interviews?

Yes, N-Queens is a classic backtracking problem frequently discussed in technical interviews, including FAANG-style interviews. It tests recursion, constraint checking, and pruning strategies.

What is the optimal approach for N-Queens?

The optimal approach uses backtracking. You place queens row by row while tracking used columns and diagonals to avoid conflicts. This allows early pruning of invalid placements and efficiently explores valid board configurations.

What data structure is best for solving N-Queens?

Common implementations use sets or boolean arrays to track occupied columns, main diagonals, and anti-diagonals. These structures allow constant-time checks to determine whether placing a queen is safe.

Previous Problem

Group Anagrams

Next Problem

Maximum Subarray

queens
)
{
6
for
(
int
i
=
0
;
i
<
row
;
++
i
)
{
7
int
placedQueenCol
=
queens
[
i
]
;
8
if
(
placedQueenCol
==
col
||
9
placedQueenCol
-
col
==
row
-
i
||
10
placedQueenCol
-
col
==
i
-
row
)
{
11
return
false
;
12
}
13
}
14
return
true
;
15
}
16
17
void
solve
(
int
n
,
int
row
,
int
*
queens
,
char
*
*
board
,
int
*
resultCount
,
char
*
*
*
*
results
)
{
18
if
(
row
==
n
)
{
19
*
results
=
realloc
(
*
results
,
sizeof
(
char
*
*
)
*
(
*
resultCount
+
1
)
)
;
20
char
*
*
newSolution
=
(
char
*
*
)
malloc
(
sizeof
(
char
*
)
*
n
)
;
21
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
22
newSolution
[
i
]
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
n
+
1
)
)
;
23
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
24
newSolution
[
i
]
[
j
]
=
board
[
i
]
[
j
]
;
25
}
26
newSolution
[
i
]
[
n
]
=
'\0'
;
27
}
28
(
*
results
)
[
*
resultCount
]
=
newSolution
;
29
(
*
resultCount
)
++
;
30
return
;
31
}
32
33
for
(
int
col
=
0
;
col
<
n
;
++
col
)
{
34
if
(
isValid
(
row
,
col
,
queens
)
)
{
35
queens
[
row
]
=
col
;
36
board
[
row
]
[
col
]
=
'Q'
;
37
solve
(
n
,
row
+
1
,
queens
,
board
,
resultCount
,
results
)
;
38
board
[
row
]
[
col
]
=
'.'
;
// backtrack
39
}
40
}
41
}
42
43
char
*
*
*
solveNQueens
(
int
n
,
int
*
returnSize
)
{
44
char
*
*
*
results
=
(
char
*
*
*
)
malloc
(
sizeof
(
char
*
*
)
)
;
45
*
returnSize
=
0
;
46
int
*
queens
=
malloc
(
sizeof
(
int
)
*
n
)
;
47
char
*
*
board
=
malloc
(
sizeof
(
char
*
)
*
n
)
;
48
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
49
board
[
i
]
=
malloc
(
sizeof
(
char
)
*
(
n
)
)
;
50
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
51
board
[
i
]
[
j
]
=
'.'
;
52
}
53
}
54
solve
(
n
,
0
,
queens
,
board
,
returnSize
,
&
results
)
;
55
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
56
free
(
board
[
i
]
)
;
57
}
58
free
(
board
)
;
59
free
(
queens
)
;
60
return
results
;
61
}
62
63
int
main
(
)
{
64
int
n
=
4
;
65
int
returnSize
;
66
char
*
*
*
results
=
solveNQueens
(
n
,
&
returnSize
)
;
67
for
(
int
i
=
0
;
i
<
returnSize
;
++
i
)
{
68
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
69
printf
(
"%s\n"
,
results
[
i
]
[
j
]
)
;
70
}
71
printf
(
"\n"
)
;
72
}
73
return
0
;
74
}
n
:
int
)
-
>
List
[
List
[
str
]
]
:
4
def
dfs
(
queens
,
xy_diff
,
xy_sum
)
:
5
p
=
len
(
queens
)
6
if
p
==
n
:
7
result
.
append
(
queens
)
8
return
None
9
for
q
in
range
(
n
)
:
10
if
q
not
in
queens
and
p
-
q
not
in
xy_diff
and
p
+
q
not
in
xy_sum
:
11
dfs
(
queens
+
[
q
]
,
xy_diff
+
[
p
-
q
]
,
xy_sum
+
[
p
+
q
]
)
12
result
=
[
]
13
dfs
(
[
]
,
[
]
,
[
]
)
14
return
[
[
"."
*
i
+
"Q"
+
"."
*
(
n
-
i
-
1
)
for
i
in
sol
]
for
sol
in
result
]
15
16
solutions
=
solveNQueens
(
4
)
17
print
(
solutions
)

Explanation

This Python implementation enhances the typical backtracking with bit positioning. The current column and diagonals are represented with list-formed negative and positive differential tracking to accommodate queen positions and check constraints efficiently.