Visualized DSA Strategies
Episode 3 Prefix Sums
Do you need this episode?
4 quick questions on what it teaches. Get them all right and skip ahead; miss one and you'll see the part of the video that answers it.
The complete guide to prefix sums for coding interviews, in JavaScript. First how a prefix array is built and why it has one extra slot; the four questions a running total answers; what each approach costs; the JavaScript for prefix and difference arrays; and five traps. Then nine types, each worked as a small problem in full: examples, the brute force animated beside the running line of code, the complete code, every line explained, and the prefix-sum solution the same way.
What you'll learn
- How a prefix array turns any range sum into one subtraction
- Why the array has n + 1 slots and starts with 0
- Left total against right total with a running sum
- Counting subarrays that sum to k with a prefix map
- Prefix remainders and balances: the same prefix twice means a range between
- Prefix and suffix products without division
- A difference array for many range updates
- 2D prefix sums and the four-corner formula
- Running maximums from both sides: trapping rain water
Chapters
Quiz: test yourself
9 questions: the ones from the end of the video, then new code. Pick an answer to see why it's right.