Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Prefix Sum

Precompute running totals so any range sum is one subtraction.

Beginner8 problems3 interactive subtopicsO(n) time · O(n) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Seed with an empty prefix
running = 0
0123
1
2
1
3
PREFIX SUMS SEEN (SUM: COUNT)
0: 1
  • active this step
  • result
  • inside the window
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [1, 2, 1, 3], k = 3
1def subarray_sum(nums, k):
2 count, running = 0, 0
▶3 seen = {0: 1}
4 for x in nums:
5 running += x
6 count += seen.get(running - k, 0)
7 seen[running] = seen.get(running, 0) + 1
8 return count
STEP 1 OF 5Setup

Seed with an empty prefix

seen = {0: 1} lets a subarray that starts at index 0 be counted.

VARIABLES

x
-
running
0
count
0
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

prefix[i] stores the sum of the first i elements, so the sum of nums[a..b] is prefix[b + 1] − prefix[a].

Why it matters

Many range-sum queries drop from O(n) each to O(1) each after an O(n) setup.

Think of it like this

A car’s odometer: the distance for any stretch is the end reading minus the start reading.

Use it when

  • Many range-sum queries on data that doesn’t change
  • Counting subarrays with a given sum (with a hash map)
  • Inputs with negative numbers, unlike windows

Avoid it when

  • The array changes often - use a Fenwick or segment tree
  • A single query - summing directly is simpler

Complexity cheatsheet

OperationTimeSpace
Build the prefix arrayO(n)O(n)
Range-sum queryO(1)O(1)
Count subarrays equal to k (with a map)O(n)O(n)

Often confused with: sliding window - it needs validity that changes monotonically; prefix sums don’t.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

range sumsubarray sum equals kcumulativebetween i and jpivot index
  • “Count subarrays summing to k” means prefix sums plus a count map
  • Negative numbers rule out sliding window, not prefix sums
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Forgetting the {0: 1} seed - subarrays starting at index 0 are missed
  • Off-by-one errors between prefix[i] and prefix[i + 1]
  • Updating seen before counting - with k = 0 it counts empty subarrays

Edge cases to test

  • k = 0
  • Negative numbers
  • A single element equal to k
Interview tip: Write sum(a..b) = prefix[b + 1] − prefix[a] on the board first - everything follows from it.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Why seed the map with {0: 1}?

06

Practice

Apply what you just watched on real interview problems.

Generate Prefix Sum problem in AI DSA Practice →
  • BeginnerRange Sum Query - ImmutableLeetCode 303 ↗ (opens in a new tab)
  • IntermediateSubarray Sum Equals KLeetCode 560 ↗ (opens in a new tab)
  • AdvancedShortest Subarray with Sum at Least KLeetCode 862 ↗ (opens in a new tab)
Sliding WindowFast & Slow Pointers