Sum Campaign Budgets Under a Shared Label Prefix

Last updated: November 29, 2026

Quick Overview

Implement insert(key, value), which sets or overwrites the value for a label, and sum(prefix), which returns the total of values for all labels starting with that prefix.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Implement insert(key, value), which sets or overwrites the value for a label, and sum(prefix), which returns the total of values for all labels starting with that prefix.

A Google Ads flavored phone screen question where campaign labels are hierarchical strings. The interviewer checks that you store running sums at trie nodes and correctly handle overwriting an existing key with a delta.

What the Interviewer Expects
  • Store a running sum at each trie node
  • Apply the difference when a key is overwritten
  • Keep a side map of current values per key
  • Answer sum in O(prefix length)
  • Explain why scanning all keys per query does not scale
Key Topics to Cover
trie
prefix sums
hash map
design
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
  • How would you support delete(key)?
  • What if values can be updated concurrently?
  • How would you return the top label by value under a prefix?
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

insert(key, val): set the value for key, replacing any previous value. sum(prefix): return the sum of current values over all keys that start with prefix, or 0 if none....

High-Level Design

The slow approach iterates every key on each sum, costing O(keys x length). Instead, give each trie node a total that equals the sum of values of all keys in its subtree. On insert, compute `del...


Submit Your Answer
Markdown supported