Maximum Ad Revenue With a Rolling-Window Cap
Updated
Problem statement
You are given n video-ad offerings. Offering i occurs at the fixed point timestamp times[i], measured in elapsed minutes, and each selected copy contributes revenues[i]. The timestamps are strictly increasing; their original gaps remain unchanged when offerings are skipped.
Choose copies according to allowRepeats:
- If
allowRepeatsisfalse, select zero or one copy of each offering. - If
allowRepeatsistrue, select any nonnegative whole number of copies of each offering. - Every copy of offering
icontributes its full revenue at the original timestamptimes[i]. Copies cannot be moved to other timestamps or extend the timeline. The selected offerings remain in chronological order.
For every real window start s, the sum of selected revenue at timestamps inside the half-open interval [s, s + windowLength) must be at most cap. The left endpoint is included and the right endpoint is excluded. Therefore two timestamps separated by exactly windowLength cannot occur in the same window.
Return the maximum total selected revenue across the entire input. Return only the integer total, not the selected copies. The empty selection is allowed and contributes 0; empty input or no affordable offering therefore returns 0.
Function
maximumAdRevenue(times: int[], revenues: int[], windowLength: int, cap: int, allowRepeats: boolean) → intExamples
Example 1
times = [1,2,4]revenues = [8,10,3]windowLength = 3cap = 10allowRepeats = falsereturn = 11Select the 8-revenue offering at minute 1 and the 3-revenue offering at minute 4, for 11 total. Their gap is exactly 3, so no half-open three-minute window contains both. The 10-revenue offering at minute 2 cannot be combined with either other offering, so it gives only 10.
Example 2
times = [1,2,4]revenues = [8,10,3]windowLength = 3cap = 10allowRepeats = truereturn = 17Select one copy at minute 1 and three copies at minute 4, contributing 8 + 3 * 3 = 17. No three-minute window contains both timestamps. The cap permits at most three copies of the 3-revenue offering. Selecting the 10-revenue offering at minute 2 leaves no capacity for either neighboring timestamp, so that choice cannot improve 17.
Example 3
times = [0,1,10,11]revenues = [2,3,4,3]windowLength = 5cap = 6allowRepeats = truereturn = 12Select three copies of the 2-revenue offering at minute 0 and two copies of the 3-revenue offering at minute 11. Each group contributes 6; no five-minute window contains both groups. The two windows [0,5) and [10,15) partition all offerings and allow at most 6 each, proving that 12 is optimal.
Constraints
0 <= n == times.length == revenues.length <= 8.0 <= times[i] <= 10^9, andtimes[i] < times[i+1]whenever both indices exist.1 <= revenues[i] <= 100.1 <= windowLength <= 10^9.1 <= cap <= 10.allowRepeatsis a boolean. Invalid or unsorted inputs are outside the accepted domain.- All revenues are integers. Even in repeated-selection mode, an offering contributes at most
cap, so the answer is at mostn * cap <= 80.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Optimal Account BalancingAmazon · ONSITE INTERVIEW
- Count and Score Distinct Hat AssignmentsRoblox · ONSITE INTERVIEWRoblox interview questions
- Partition a Card Deck into FifteensMeta · ONSITE INTERVIEWMeta interview questions
- Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Maximize Valid TrainsBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Word Break II: All SentencesMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Get Best PriceConfluent · ONSITE INTERVIEWConfluent interview questions
- Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
- Enumerate Right-and-Down Matrix PathsOracle · ONSITE INTERVIEWOracle interview questions