Data Structures
Cumulative Values
Programming
Algorithms
Computer Science

What is a good datastructure to keep cumulative values in?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A good data structure for cumulative values depends on what operations you need. If the data is fixed and you mainly want fast range sums, prefix sums are often enough. If the values change over time and you need updates plus queries, a Fenwick tree or segment tree is usually the better answer.

Prefix Sum Array for Static Data

If the underlying data does not change often, a prefix sum array is the simplest structure.

python
1def build_prefix(values):
2    prefix = [0]
3    for value in values:
4        prefix.append(prefix[-1] + value)
5    return prefix
6
7
8def range_sum(prefix, left, right):
9    return prefix[right + 1] - prefix[left]

This gives O(1) range-sum queries after O(n) preprocessing. It is hard to beat for simplicity when the dataset is mostly read-only.

Fenwick Tree for Dynamic Prefix Sums

If values change and you still want efficient cumulative queries, a Fenwick tree, also called a Binary Indexed Tree, is a strong choice.

It supports:

  • point updates in O(log n),
  • prefix sum queries in O(log n),
  • moderate implementation complexity.

That makes it a common answer when the phrase "cumulative values" really means running totals that must remain queryable after updates.

Segment Tree for More General Range Work

A segment tree is more flexible than a Fenwick tree. It also supports O(log n) updates and queries, but it can be extended more naturally to other operations such as range minimum, maximum, or custom associative aggregates.

The tradeoff is that segment trees use more memory and are more complex to implement.

Choose by Operation Mix

A useful decision rule is:

  • static data plus many range queries: prefix sums,
  • dynamic updates plus prefix or range sums: Fenwick tree,
  • dynamic updates plus broader range operations: segment tree.

The right answer is usually not about which structure is "best" in general. It is about which operations dominate the workload.

Sometimes a Plain Running Total Is Enough

If you only need one cumulative total as new values arrive, you may not need any special tree at all.

python
total = 0
for value in stream:
    total += value

That is the best answer when the requirement is just a running aggregate and not arbitrary historical range queries.

Space and Simplicity Matter Too

A prefix sum array is often chosen not only because it is fast for queries, but because it is easy for other developers to understand immediately. Fenwick trees and segment trees are more powerful, but that extra power only pays off when the workload actually needs it. Simplicity is also part of performance engineering when maintainability matters.

Common Pitfalls

  • Building a segment tree when a prefix sum array already solves the actual problem.
  • Using prefix sums on data that changes frequently and then paying expensive rebuild costs.
  • Choosing a Fenwick tree without checking whether the required queries are more general than prefix-style sums.
  • Over-optimizing before identifying the real operation mix.
  • Confusing "cumulative total" with "arbitrary range aggregation" and picking the wrong abstraction.

Summary

  • Prefix sums are excellent for static cumulative-query problems.
  • Fenwick trees are strong when values change and prefix sums must stay efficient.
  • Segment trees are more flexible when the query set is broader.
  • For a single running total, a plain accumulator may be enough.
  • The best data structure depends on updates, query type, and workload shape.

Course illustration
Course illustration

All Rights Reserved.