Algorithms / 16 visualizations
Coding Interview Pattern Visualizers
The array patterns behind most coding interviews.
Open the lab → Every one runs on your own input, step by step, with the code highlighted in Java, Python, C++, C or JavaScript. Free, no sign-up.
All patterns visualizations
- Sliding Window (Fixed Size)O(n)
Keep one running sum. When the window slides right, add the value coming in and subtract the value going out.
- Sliding Window (Variable Size)O(n)
Grow the window with r until it meets the condition, then shrink it with l as far as it stays valid.
- Two Pointers (Opposite Ends)O(n)
Start at both ends. If the sum is too small, move the left pointer right. If too big, move the right pointer left.
- Container with Most WaterO(n)
Start with the widest container. Always move the shorter wall inward, because the taller wall can never do better with less width.
- Trapping Rain WaterO(n)
Water above a bar = the lower of (tallest wall on its left, tallest wall on its right) minus the bar. Two pointers find this without extra arrays.
- Fast and Slow PointersO(n)
fast reads every value. slow marks where the next kept value should go.
- Kadane's AlgorithmO(n)
At each value, either extend the current subarray or start fresh here, whichever is bigger. Remember the best you have seen.
- Prefix Sum + Hash MapO(n)
If prefix[j] = prefix[i] − k, the values between j and i sum to k. A hash map remembers how often each prefix sum appeared.
- Monotonic StackO(n)
The stack holds values still waiting for an answer. A bigger value pops everything smaller than it and becomes their answer.
- Dutch National FlagO(n)
lo marks where the next 0 goes, hi where the next 2 goes. mid scans the unknown part in between.
- Boyer-Moore Majority VoteO(n)
Pair every vote for the candidate with a vote against it, and cancel them. Whatever survives all the cancelling must be the majority, because it has more votes than everything else combined.
- Best Time to Buy and Sell StockO(n)
Walk the days once. Remember the cheapest price seen so far; selling today earns today minus that price. Keep the best earning.
- Product of Array Except SelfO(n)
out[i] = (product of everything left of i) × (product of everything right of i). One pass left to right stores the left products, a second pass right to left multiplies in the right products.
- Rotate an Array By KO(n)
Reverse the whole array, then reverse the first k values, then reverse the rest. The last k values end up in front, each part in its original order.
- Merge Two Sorted ArraysO(n + m)
Keep a pointer at the front of each array. Take the smaller of the two front values, move that pointer, and repeat. When one array runs out, copy the rest of the other.
- Sliding Window MaximumO(n)
A deque holds indexes whose values only go down from front to back. A new value pops every smaller value off the back, since those can never be a maximum again. The front is the window maximum; drop it once it slides out.