Prefix Sums & Intervals
Answer range queries in O(1) after linear preprocessing and process interval data by sorting and sweeping.
4 objectives 6 steps about 115 min
Check what you know
The lessons are coming later. The knowledge check is ready now.
This skill page is a public outline. Its lessons and knowledge check appear here as subject-matter experts approve them.
Learning objectives
- Build 1D and 2D prefix-sum arrays and answer range-sum queries in O(1) each.
- Count subarrays with a target or divisible sum by combining prefix sums with a hash map, including inputs with negative numbers.
- Apply difference arrays to batch range updates and compute point coverage counts.
- Merge, insert and count overlapping intervals by sorting endpoints and sweeping, for example to find the minimum number of meeting rooms.
Lesson plan
6 steps, about 115 min · 0 of 5 lessons available so far
- Precompute Once, Query Fast Concept · 15 min · coming later
- Lab: Range Sums on Lists and Grids Lab · 20 min · coming later
- Subarray Sums with a Running Map Worked example · 25 min · coming later
- Difference Arrays for Batch Updates Worked example · 15 min · coming later
- Sweeping Through Intervals Worked example · 25 min · coming later
- Check: Ranges and Intervals Knowledge check · 8 questions
Practise it
From a booking log, find the busiest hour with a difference array, merge overlapping reservations, compute the minimum number of rooms, and count runs of daily balances summing to k; pass tests with negative values and touching intervals.
References
Hard prerequisites
Unlocks
On these roadmaps
Progress and evidence for this skill are shared across all of them.