Spread a Daily Campaign Budget Across Hours With Supply Caps

Last updated: November 29, 2026

Quick Overview

Given a daily budget, a traffic forecast per hour, and a maximum spend each hour can absorb, allocate the budget proportionally to forecast while never exceeding any hour's cap, redistributing the excess to the other hours.

StackAdapt
Coding & Algorithms
Software Engineer
StackAdapt
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a daily budget, a traffic forecast per hour, and a maximum spend each hour can absorb, allocate the budget proportionally to forecast while never exceeding any hour's cap, redistributing the excess to the other hours.

Budget pacing decides how fast a StackAdapt campaign spends so it does not burn its budget at 6 a.m. and go dark for the evening audience. The interviewer wants to see the water-filling idea and a sort-based solution instead of repeated rescanning.

What the Interviewer Expects
  • Recognizes that capped hours must be fixed before computing the others' shares
  • Sorts hours by cap-to-forecast ratio to process them in one pass
  • Handles hours with zero forecast
  • Explains what happens when total capacity is below the budget
  • Gives O(H log H) complexity
Key Topics to Cover
Water-filling
Greedy
Sorting
Budget pacing
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 re-run this mid-day using actual spend so far?
  • How would you add a minimum spend per hour?
  • The forecast is wrong by 30 percent at noon; what feedback loop corrects pacing?
  • How does this interact with bid price, since lowering bids also slows spend?
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

Inputs: budget, forecast[h] >= 0, caps[h] >= 0 for H hours. Uncapped hours receive spend proportional to forecast. If an hour's proportional share would exceed its cap, it gets exactly its cap a...

High-Level Design

The hour most likely to hit its cap is the one with the smallest cap / forecast, because its share per unit of forecast hits the ceiling first. Process hours in that order: if the current remaining ...


Submit Your Answer
Markdown supported

Related Questions