Sum Ad Budgets Across Every Campaign Name Sharing a Prefix
Last updated: November 29, 2026
Quick Overview
Implement a structure where set_budget(name, amount) assigns or overwrites a campaign's daily budget and prefix_total(prefix) returns the sum of budgets for all campaigns whose name starts with prefix.
Meta
November 29, 20260
0
0 solved
Implement a structure where set_budget(name, amount) assigns or overwrites a campaign's daily budget and prefix_total(prefix) returns the sum of budgets for all campaigns whose name starts with prefix.
Meta Ads Manager groups campaigns by naming convention (for example, every campaign beginning with 'us_spring_'), and rolling up spend by name prefix is a practical warm-up. The interviewer checks that overwrites are handled with a delta rather than double counting.
What the Interviewer Expects
- Store a running sum on each trie node
- Track each campaign's current budget so overwrites apply a delta
- Return 0 for a prefix with no match
- Keep both operations linear in the string length
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
- How would you support deleting a campaign?
- How would you return the top three campaigns by budget under a prefix?
- What if prefixes are matched on underscore-separated segments only?
- How would this work if budgets change thousands of times a second?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Names are strings of letters, digits, and underscores; amounts are integers.
- Setting an existing name replaces its budget.
prefix_total('')returns the total of all budgets....
High-Level Design
Keep a dict current[name] and a trie where each node stores total, the sum of budgets of all names passing through it. On set_budget, compute delta = amount - current.get(name, 0), save the ne...