Live demo: Spotify shuffle
See how it works. Step by step.
Interactive models of the algorithms behind your apps and your coding interviews. Each one runs real code, so you can change the input and see what happens.
- D1
- A3
- B4
- A1
- B3
- B1
- A4
- A6
- D2
- C1
- C2
- A2
- D3
- B2
- A5
- C3
- CH1 Artist A
- CH2 Artist B
- CH3 Artist C
- CH4 Artist D
fisherYates() on 16 songs, seed 0x3F2A. Done: the same artist plays back to back 3 times. Outlined pairs are the repeats.
Table
| Same artist back to back, per shuffle | Pure random | Spread out |
|---|---|---|
| 0 | 1.5% (30) | 57.8% (1155) |
| 1 | 9.1% (182) | 38.0% (759) |
| 2 | 18.9% (378) | 4.3% (86) |
| 3 | 26.4% (528) | 0.0% (0) |
| 4 | 22.2% (444) | 0.0% (0) |
| 5 | 13.1% (262) | 0.0% (0) |
| 6 | 5.8% (116) | 0.0% (0) |
| 7 | 2.4% (47) | 0.0% (0) |
| 8+ | 0.7% (13) | 0.0% (0) |
Interview prep
Prep for coding interviews
48 classic interview problems you can step through, edit and test. In Trapping Rain Water, two pointers walk in from the ends, and each bar holds water up to the shorter of the tallest walls on either side.
Under the hood
Every demo runs real code
The play order at the top of the page comes from the function below. It runs again each time you shuffle or move the slider. Every model on codebrew works this way, including the five below.
Shuffle runs fisherYates()
function fisherYates<T>(
songs: readonly T[],
rand: () => number,
trace?: Trace<T>
): T[] {
const order = songs.slice()
for (let i = order.length - 1; i > 0; i--) {
const j = Math.floor(rand() * (i + 1))
trace?.("pick", i, j, order) // to the stage
;[order[i], order[j]] = [order[j]!, order[i]!]
trace?.("swap", i, j, order) // to the stage
}
return order
}Spread out runs spreadShuffle()
function spreadShuffle<T extends { artist: string }>(
songs: readonly T[],
rand: () => number,
trace?: (artist: string, m: number, offset: number, placed: readonly { song: T; pos: number }[]) => void
): T[] {
// Space each artist's songs evenly across the playlist,
// starting at a random offset, with a little jitter.
const byArtist = new Map<string, T[]>()
for (const song of songs) {
if (!byArtist.has(song.artist)) byArtist.set(song.artist, [])
byArtist.get(song.artist)!.push(song)
}
const placed: { song: T; pos: number }[] = []
for (const [artist, mine] of byArtist) {
const m = mine.length
const order = fisherYates(mine, rand)
const offset = rand() / m
order.forEach((song, i) => {
const jitter = (rand() - 0.5) * (0.2 / m)
placed.push({ song, pos: offset + i / m + jitter })
})
trace?.(artist, m, offset, placed) // report the step to the stage
}
placed.sort((a, b) => a.pos - b.pos)
return placed.map((p) => p.song)
}Python visualizer
Step through your own code
Paste a Python function and watch it run line by line: lists with their pointers, graphs as they're searched, recursion as a tree of calls. It runs in your browser, so your code stays on your machine.
Explainers
How everyday tech works
Shazam, Face ID, GPS, ChatGPT and more, as models you can run and change.
Algorithms
Learn the patterns behind the problems
Most problems reuse a few patterns: a sliding window, a grid that fills in, a search that spreads. Learn the pattern, then step through the problems that use it.
Hashing & Frequency Maps
Runs liveTrade memory for O(1) lookups, counts, and dedupe.
Dynamic Programming
Runs liveBreak a problem into overlapping subproblems and reuse the answers.
Graph Traversal (BFS & DFS)
Runs liveVisit every node once: breadth-first for shortest paths, depth-first for structure.
Two Pointers
Runs liveWalk a sorted array from both ends to skip impossible work.
Binary Search
Runs liveHalve the range that can hold the answer, on a sorted array or a range of answers.
Sliding Window
Runs liveTurn nested-loop scans of contiguous ranges into linear time.
Popular problems
Requests
What should we explain next?
Tell us what you've always wondered about. We build the most requested ones next.