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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

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
trie
prefix sums
hash map
updates
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported