Skip to the workspace
Pebellslearn by building
Prefix Sums & Range Queries  ›  Lesson

Prefix Sums, Difference Arrays & Windows

Prefix Sums & Difference Arrays 30 minAdd everything once so every range question is one subtraction

dsa / Prefix Sums, Difference Arrays & Windows

Explore each scene at your own pace.

01 · The brute force, and what it costs

The brute force, and what it costs

Prefix Sums, Difference Arrays & Windows

Definition
A prefix sum array stores the running total of a list, so the sum of any range is one subtraction of two stored totals. A difference array does the reverse, turning a range update into two point changes.
In plain words
Keep a running tally of everything counted so far. To know how much happened between minute 3 and minute 9, subtract the tally at minute 3 from the tally at minute 9 instead of adding up every minute in between.

In short: build a running-total row once, P[i + 1] = P[i] + a[i], and any range total becomes one subtraction, P[r + 1] − P[l]. The spare P[0] = 0 cell means a range starting at minute 0 needs no special case. The same trick runs backwards: a difference array turns a wide range update into two point writes, with one final sweep — itself a prefix sum — turning differences back into values. Both only work because addition can be undone; min has no inverse, which is why the rest of this section exists.

A prefix row P with a spare zero cell; P[1] and P[5] are highlighted and subtracted to total minutes 1..4

From this section on you build Quarry, a search-and-storage engine: the kind of program that sits under a dashboard, keeps a log of everything that happened, and answers questions about it fast. Later sections give it an index, a text search and a storage layer. This section gives it an analytics engine.

The raw material is simple. Quarry keeps one number per minute: how many requests arrived, how many errors, how many bytes were written. A day is 1,440 minutes, a month is 43,200, and the dashboard asks the same shape of question over and over:

How many errors between minute l and minute r?

Every lesson in this section answers that question under a harder rule. Here the log never changes after it is written. Next lesson it gets edited. By the end it takes range updates, minimum queries and lives on disk.

The brute force, and what it costs

The obvious answer is a slice and a sum:

python

Each query reads every minute it covers. One query over a month is 43,200 reads; a dashboard with twenty panels refreshing every few seconds asks thousands of them. That's O(n) per query and O(n·q) in total, and nearly all of it is re-adding numbers that were added a moment ago by the previous query.

Scene 1 of 10
Project context and further resources
You're building a piece ofQuarry — the analytics engine
This piece — range_totals(): Answers any 'how many between minute l and r' in O(1) from a prefix row.
Scenario Quarry's dashboard asks how many errors happened between two minutes, thousands of times a second. Rescanning the log for each panel is too slow, so the analytics engine builds one prefix row and answers every panel with a subtraction.
range_totals.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
range_totals(counts, queries) → listThe total of each [l, r] range of minutes, both ends included.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.