VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 08: Recursion and backtracking

Lesson 8.9 of 10 in this module Unrolled Pro

N-Queens

Place n queens on an n × n board so none attack each other, and list every way. From trying every arrangement of one queen per row and checking at the end (n^n boards), to backtracking row by row with a scan for conflicts, to sets of used columns and diagonals for O(1) checks, to bitmasks that count all solutions of n = 12 in a fraction of a second. Then one solution for n = 200 with min-conflicts local search, and the general lesson for constraint problems such as scheduling jobs on GPUs.

about 10 min 5 Python examples 2 quick checks

Problem. Place n queens on an n × n chessboard so that no two share a row, a column or a diagonal. Return all distinct solutions, each as a list of strings (Q for a queen, . for empty).

text
n = 4   →   . Q . .      . . Q .
            . . . Q      Q . . .
            Q . . .      . . . Q
            . . Q .      . Q . .

This is the classic constraint satisfaction problem: every choice (a queen's column in a row) restricts all later choices, and the art is detecting a dead end as early and as cheaply as possible.

ML context. Constraint problems are everywhere in ML infrastructure: placing jobs on GPUs so that no two memory-hungry jobs share a device, assigning model shards to machines under bandwidth limits, scheduling evaluation runs that can't overlap on the same accelerator, generating test cases that satisfy a set of rules. The toolkit is the same: backtracking with O(1) conflict checks when you need every answer, and local search when you need one good answer for a large instance (Change B).


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "N-Queens"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee