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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

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
Fenwick Tree
Prefix Sums
Segment Tree
Bit Manipulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: integer array a of length n.
  • update(i, v): set a[i] = v (0-based).
  • query(l, r): sum of a[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 ...


Submit Your Answer
Markdown supported