Range Revenue Totals over Daily Ad Spend with Point Updates
Last updated: November 29, 2026
Quick Overview
Given an array of daily ad spend, support update(i, value) to correct a single day and query(l, r) to return the total spend from day l to day r inclusive, both in sublinear time.
0
0
0 solved
Given an array of daily ad spend, support update(i, value) to correct a single day and query(l, r) to return the total spend from day l to day r inclusive, both in sublinear time.
Framed around Google Ads reporting where late-arriving corrections update individual days while dashboards query arbitrary ranges. The interviewer is checking that you know the prefix sum versus mutable trade-off and can implement a Fenwick tree (or segment tree) from memory without index bugs.
What the Interviewer Expects
- Explains why plain prefix sums make updates O(n)
- Implements a Fenwick tree with correct lowbit arithmetic and 1-based indexing
- Converts set-to-value into a delta update
- Builds the tree in O(n) or O(n log n) and says which
- Compares with a segment tree and when to prefer one
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Support adding a value to an entire range of days, with point queries.
- Support range add and range sum together.
- What if days are sparse timestamps over many years instead of a dense array?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: integer array
aof length n. update(i, v): seta[i] = v(0-based).query(l, r): sum ofa[l..r]inclusive.- Both in O(log n); many interleaved operations....
High-Level Design
A Fenwick tree (binary indexed tree) stores at position i (1-based) the sum of the block of length i & -i ending at i. A prefix sum walks i downward by stripping the lowest set bit; an update walks ...