Talentd
FleetCode
Make and follow sheets.Sign in

Prefix Sum Problems (252)

Problems tagged with Prefix Sum. Start with the easy ones, then move to medium and hard.

20 Easy155 Medium77 Hard

Learn these first

1
Array

Prefix sum is usually built on arrays. Understanding array traversal, indexing, and subarray concepts helps you construct and query prefix sums correctly.

2
Hash Table

Many prefix sum interview problems store previously seen prefix values in a hash map to detect subarrays with a given sum in O(n) time.

3
Sliding Window

Both techniques optimize subarray problems. Knowing sliding window helps you recognize when prefix sums or window-based approaches are more appropriate.

4
Binary Indexed Tree

Fenwick Trees extend the prefix sum idea to support efficient updates and range queries, making them a natural next step after mastering static prefix sums.

TitleSolutionAcceptDifficultyTopics
Solution53.1%Medium
Array, Binary Search +2· 18 companies
Solution69.3%Medium
Array, Prefix Sum· 40 companies
253. Meeting Rooms IIVideoPremium
Solution52.6%Medium
Array, Greedy +4· 42 companies
Solution73.5%Easy
Array, Design +1· 8 companies
Solution59.2%Medium
Array, Design +2· 10 companies
Solution50.8%Medium
Array, Hash Table +1· 8 companies
370. Range AdditionVideoPremium
Solution73.0%Medium
Array, Prefix Sum· 2 companies
Solution61.2%Hard
Array, Binary Search +3· 20 companies
Solution31.3%Medium
Array, Hash Table +2· 9 companies
Solution52.2%Medium
Array, Hash Table +1· 16 companies
Solution49.0%Medium
Array, Binary Search +3· 26 companies
Solution50.1%Hard
Array, Hash Table +1· 1 company
Solution48.2%Medium
Array, Hash Table +1· 47 companies
Solution37.7%Hard
Array, Binary Search +1· 1 company
Solution54.8%Medium
Array, Binary Search +2· 20 companies
Solution64.2%Easy
Array, Prefix Sum· 18 companies
731. My Calendar IIVideo
Solution63.3%Medium
Array, Binary Search +4· 3 companies
732. My Calendar IIIVideo
Solution72.4%Hard
Binary Search, Design +3· 1 company
Solution55.5%Medium
Array, Dynamic Programming +1· 2 companies
Solution46.1%Medium
Array, Prefix Sum +1· 3 companies
Solution69.7%Medium
Array, Hash Table +2· 7 companies
Solution56.7%Medium
Array, Hash Table +1· 11 companies
Solution68.6%Medium
Array, Binary Search +2· 20 companies
1094. Car PoolingVideo
Solution56.5%Medium
Array, Heap (Priority Queue) +3· 12 companies
Solution68.7%Medium
Array, Prefix Sum· 5 companies
Solution38.5%Medium
Array, Hash Table +3· 2 companies
1140. Stone Game IIVideo
Solution75.8%Medium
Array, Dynamic Programming +3· 5 companies
Solution75.5%Medium
Array, Hash Table +3· 9 companies
Solution78.1%Medium
Array, Bit Manipulation +1· 3 companies
1314. Matrix Block SumVideo
Solution77.0%Medium
Array, Matrix +1· 3 companies
Solution58.1%Medium
Array, Prefix Sum +1· 11 companies
Solution86.9%Easy
Array, Prefix Sum· 6 companies
Showing 1–50 of 252

More about Prefix Sum

Prefix Sum is a powerful technique in data structures and algorithms used to efficiently compute sums of subarrays or ranges. Instead of recalculating the sum for every query, a prefix sum array stores cumulative sums so that any range sum can be computed in constant time. If prefix[i] represents the sum of elements from index 0 to i, then the sum of any range (l, r) can be calculated quickly using prefix[r] − prefix[l − 1]. This simple idea transforms many problems that would normally take O(n) per query into O(1) operations after preprocessing.

In coding interviews, prefix sum appears frequently because it helps optimize problems involving subarrays, range queries, and cumulative counts. Many well-known interview questions rely on combining prefix sums with data structures like Hash Table to detect subarrays with a given sum, or with techniques such as Sliding Window and Two Pointers for efficient range-based processing. Since most prefix sum problems start with an Array, mastering array traversal and indexing is essential.

Common prefix sum patterns include:

  • Computing subarray sums in O(1) after preprocessing
  • Counting subarrays with a specific sum using hash maps
  • Handling range queries efficiently
  • Building 2D prefix sums for matrix region queries
  • Optimizing brute-force O(n²) solutions into O(n)

Prefix sums are also closely related to advanced structures like Binary Indexed Tree and Segment Tree, which extend the idea to support dynamic updates and complex range queries. Understanding the prefix sum pattern gives you the foundation needed to approach these more advanced data structures.

On FleetCode, you can practice ranging from beginner-friendly subarray questions to advanced interview challenges. By solving progressively harder problems, you’ll learn how to recognize when prefix sums apply and how to combine them with other algorithmic techniques to pass real coding interviews.

Prefix Sum: common questions

What are Prefix Sum problems in DSA?

Prefix Sum problems involve precomputing cumulative sums of an array so that range queries or subarray sums can be calculated in constant time. Instead of recalculating sums repeatedly, the algorithm stores running totals and uses simple subtraction to compute results quickly.

What is the best way to learn Prefix Sum?

Start by understanding the basic prefix sum array and how to compute range sums in O(1). Then practice problems involving subarray sums with hash maps, followed by advanced cases like 2D prefix sums and range query optimizations.

Is Prefix Sum important for FAANG interviews?

Yes. Prefix Sum is a common optimization pattern used in many FAANG interview questions involving subarrays, cumulative counts, and range queries. It often appears in medium-level array and hash map problems.

What are common Prefix Sum patterns in coding problems?

Common patterns include subarray sum detection, counting the number of subarrays with a given sum, range sum queries, prefix sum with hash maps, and 2D matrix prefix sums for region calculations.

What are the best Prefix Sum problems for coding interviews?

Popular interview problems include Subarray Sum Equals K, Range Sum Query, Continuous Subarray Sum, and 2D Range Sum Query. These questions test your ability to combine prefix sums with hash maps, arrays, and optimization techniques.