Playground
Visualizers
Every interactive figure from the lessons. Change the input, press Run, then step with the buttons or the ← → keys. Each step explains what the algorithm decided and why.
Go internals
var s []int: a nil slice. No array is allocated until the first append.
a := []int{1, 2, 3, 4, 5}: len 5, cap 5.
| variable | address | value |
|---|---|---|
| main: x | 0xc000010 | 10 |
x := 10. x lives at some address, shown here as 0xc000010.
The function starts. Nothing is deferred yet.
ch := make(chan int, 2). The producer goroutine will send 4 values; main receives with for v := range ch.
Arrays & strings
Look for the first index whose value is ≥ 9. Every index in [lo, hi) = [0, 7) is still a candidate.
Start at both ends. Each step discards one element that cannot be part of the answer.
First window: 2 + 1 + 5 = 8.
'a' is new to the window. Window "a" has length 1.
pre[0] = 0: the sum of no elements.
cur = best = -2: the best subarray ending at index 0 is just [-2].
h[l]=0 < h[r]=1: the right side has a wall at least as tall, so the left max (0) decides. Water at 0: 0 - 0 = 0.
Building the table: "ab" has no prefix that is also a suffix. lps[1] = 0.
Stacks, queues & hashing
73 is not warmer than the top of the stack. Push day 0; it waits.
Push 0. The first window is not full yet.
10 empty slots. hash(k) = k mod 10.
Empty cache with room for 3.
Sorting
5 > 2: swap them.
Scanning for the smallest from index 0: smallest so far is 5, looking at 2.
Take 2. Everything left of index 1 is already sorted.
Merge [5] and [2], both already sorted.
Partition indexes 0..7 around pivot 4.
Trees & heaps
Insert 8 as the root.
Push 5: append it at index 0, the next free spot on the bottom level.
Add 5 to low (it is ≤ low's max). Median = top of low = 5.
Built bottom-up: each leaf is one element, each parent is the sum of its two children. Now query sum(2..6).
Graphs
| S | |||||||
| E |
Start with S in the queue at distance 0. Numbers show the distance from S; outlined cells are waiting in the queue.
| build | deploy | go | lint | test | |
|---|---|---|---|---|---|
| in-degree | 1 | 2 | 0 | 0 | 2 |
Count incoming edges. Nodes with in-degree 0 (go, lint) have no prerequisites and start in the queue.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| dist | 0 | ∞ | ∞ | ∞ | ∞ |
| via | – | – | – | – | – |
dist[A] = 0, everything else ∞. The heap holds (A, 0).
Every item starts as its own root: parent[i] = i.
| A-C | B-C | D-E | A-B | B-D | C-D | C-E | |
|---|---|---|---|---|---|---|---|
| weight | 1 | 2 | 3 | 4 | 5 | 8 | 9 |
Sort all edges by weight. Every node starts in its own group.
Dynamic programming & math
dp[0] = 0: zero coins make amount 0. Every other amount starts as ∞ (impossible so far).
| ∅ | b | d | c | a | b | a | |
|---|---|---|---|---|---|---|---|
| ∅ | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| a | 0 | ||||||
| b | 0 | ||||||
| c | 0 | ||||||
| b | 0 | ||||||
| d | 0 | ||||||
| a | 0 | ||||||
| b | 0 |
Row 0 and column 0 are empty prefixes: their LCS is 0.
10 is bigger than every tail: it extends the longest subsequence. Length is now 1.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
| 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
| 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
| 60 |
0 and 1 are not prime. Start with 2.
System design
The bucket starts full with 3 tokens, refilling 1/s.
Each server is hashed to a point on a 360° ring.
| sign | timestamp (41 bits) | machine (10) | sequence (12) |
|---|---|---|---|
| 0 | 00000000000000111010110111100110100010101 | 0000100101 | 000000000101 |
| 1 bit | 123456789 ms | 37 | 5 |
The top bit stays 0 so the ID is positive in a signed int64.
125000000 = 2016129 × 62 + 2. Remainder 2 is '2'. Prepend it; continue with 2016129.