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

Lesson 8.6 of 10 in this module Unrolled Pro

Permutations

All orderings of n distinct items: n! of them. From generating all n^n sequences and filtering (n^n is far larger than n!), to backtracking that checks 'already used?' by scanning the path, to a used array that makes the check O(1), and swapping in place. Then duplicates in the input, the next permutation in lexicographic order without recursion, jumping straight to the k-th permutation, and sampling a uniformly random ordering with a correct shuffle when n! is too large to enumerate.

about 10 min 6 Python examples 2 quick checks

Problem. Given nums of distinct integers, return all possible orderings.

text
nums = [1, 2, 3]   →   [1,2,3] [1,3,2] [2,1,3] [2,3,1] [3,1,2] [3,2,1]
nums = [0, 1]      →   [0,1] [1,0]

There are n! orderings, each of length n, so O(n · n!) is the floor. It grows brutally: 10! = 3.6 million, 13! = 6 billion. Past n ≈ 10 you don't enumerate orderings, you sample or search them (Changes C and D).

ML context. Orderings appear as data shuffles (every epoch should see a fresh uniformly random order), curriculum orderings, the order of preprocessing steps, the order layers are assigned to devices, and permutation tests (shuffle the labels to see if a metric beats chance). Almost always you need a random permutation or a few specific ones, not all n!, and getting the random one exactly uniform has a classic pitfall.


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 "Permutations"

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