Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Count the safe queen placements

    HardAbout 40 minFull AccessLearn it: N-Queens: the famous one, briefly

    The task

    A chess puzzle site rates its puzzles by how many solutions they have. One family of puzzles asks for n queens on an n × n board, none of them attacking another.

    Complete CountQueenPlacements(int n). Return how many ways n queens can be placed on an n × n board so that no two share a row, a column or a diagonal. Throw ArgumentOutOfRangeException for n below 1.

    Signature

    public static int CountQueenPlacements(int n)

    Examples

    • 5 → 10. Ten placements work on a 5 × 5 board.
    • 2 → 0. On a 2 × 2 board any two queens attack each other.

    Limits: n from 1 to 12.

    The tests check: n from 1 to 10, the boards with no placement (2 and 3), n = 12 in time, and n = 0 refused.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // How many ways n queens fit on an n x n board with no two sharing a row, column or diagonal.
          public static int CountQueenPlacements(int n)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.