Prepin
Log in

Display every potential solution to the N Queens problem.

Approach / Explanation

The N Queens problem is a well-known challenge in computer science. It seeks to find all unique arrangements of N queens on an NxN chess board in such a way that no two queens can attack each other, meaning no two queens occupy the same row, column, or diagonal. This problem typically employs a backtracking approach, which should be explained through the use of a recursive backtracking algorithm. Important points to consider when addressing this problem: Recursive and Backtracking Method : This is the fundamental technique necessary for solving the problem. You should examine each available position for a queen in a specific row, and if it does not result in a solution, backtrack to explore the next position. Board Representation : It's crucial to determine how the board will be represented. A common method is to utilize a 2D array. Safe Position Verification : You will need a way to verify if a queen can be placed in a particular square within the current column.

Suggested Answer

To address this issue, I would likely employ a recursive approach with backtracking. Initially, I would set up an empty NxN 'board'. Starting with the first row, I would position a queen in a column that is not threatened by any other queen. If there is no available column, I would return false. When a valid position is identified, I would mark that cell on the board as part of the solution and then recursively verify if this leads to a valid solution for the remaining rows. If placing the queen in the current row results in a solution, I would return true. If it does not, I would backtrack by removing the queen from its current location and attempt to place it in a different position within the same row, repeating the process as necessary.

Alternative Answer

A Python implementation for this issue may resemble the following: def solveNQueens ( N ) : def DFS ( queens , xy_diff , xy_sum ) : p = len ( queens ) if p == N : result . append ( queens ) return None for q in range ( N ) : if q not in queens and p - q not in xy_diff and p + q not in xy_sum : DFS ( queens + [ q ] , xy_diff + [ p - q ] , xy_sum + [ p + q ] ) result = [ ] DFS ( [ ] , [ ] , [ ] ) return [ [ "." * i + "Q" + "." * ( N - i - 1 ) for i in sol ] for sol in result ] This function employs depth-first search. The 'queens' variable is a list containing N values, each representing the column index where a queen is positioned. The lists xy_diff and xy_sum signify the diagonals. The variable 'p' indicates the current row index. It checks if placing a queen at a specific position is feasible and then recursively examines the subsequent rows. If it is not possible, it refrains from placing the queen there and proceeds to the next column in the same row.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.