N-Queens

hard
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 25. Everything before it counts as cleared.

Place n queens on an n × n board so that no two attack each other (same row, column or diagonal). This is the classic backtracking problem.

Implement:

  • solve_n_queens(n): all solutions, each a list of column indices by row ([1, 3, 0, 2] means row 0 has its queen in column 1, and so on), in lexicographic order.
  • count_n_queens(n): just the number of solutions, computed without building every board. Use sets (or bitmasks) for the occupied columns and diagonals.
  • render(solution): a list of strings like ".Q..".
#backtracking#recursion2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
N-Queens · TechGradify