AdvancedAbout 30 min

    Backtracking (N-Queens)

    Read the full article on Substack

    6 min read.

    Solve complex problems by building candidates step-by-step and abandoning them ("backtracking") as soon as they fail constraints.

    Think of it this way: Think of trying to solve a Sudoku puzzle. You fill in a number. If it works, you move to the next square. If you get stuck later, you erase your last move and try a different number.

    0123401234

    Place 5 queens on a 5×5 board, one per row, so none attack each other.

    1/18

    Check your understanding

    Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.

    N-Queens

    Sequence

    Order the steps of the backtracking search.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    What distinguishes Backtracking from Brute Force?

    Done with this lesson?

    Sign in free to keep your progress, streak and quiz scores.

    Keep going