DDSA Solutions

Prefix Sum

38 problems · 26 with full explanations

5 Easy20 Medium2 Hard
A prefix sum array pre-computes cumulative values so any range query is answered in O(1). The classic sum of range [l, r] = prefix[r+1] - prefix[l]. 2D prefix sums extend this to matrix sub-rectangle queries. Combine with a hash map for "subarray sum equals k" problems.

How to practice

To practice Prefix Sum problems effectively, start with the Easy problems listed below, trace through each solution on paper, then re-implement without looking. When you can recognise the prefix sum pattern within 30 seconds of reading a new problem, move on to Medium difficulty. Use the related topic pages and our study guide for a structured progression.

Open the full study guide →

Start here (Easy + explained)

All Prefix Sum problems