Maximize Ad Revenue Without Filling Two Adjacent Feed Slots
Last updated: November 29, 2026
Quick Overview
Each slot in a Discover feed has an expected ad revenue, but policy forbids ads in two adjacent slots. Return the maximum total revenue you can earn by choosing which slots get ads.
0
0
0 solved
Each slot in a Discover feed has an expected ad revenue, but policy forbids ads in two adjacent slots. Return the maximum total revenue you can earn by choosing which slots get ads.
This is the house-robber pattern dressed in a Google ads policy, a frequent phone-screen opener. The interviewer wants the take-or-skip recurrence, an O(1) memory version, and a quick explanation of why greedy picking of the largest values fails.
What the Interviewer Expects
- Formulate take-or-skip at each slot
- Give a counterexample to the greedy approach
- Implement in O(n) time and O(1) space
- Handle empty and single-slot inputs
- Optionally recover which slots were chosen
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
- What if the feed is circular, so the first and last slots are adjacent?
- What if the rule is at least two empty slots between ads?
- How would you return the chosen slot indices?
- Revenue estimates can be negative. Does anything change?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list rev of non-negative numbers, one per slot (possibly empty). Choose a subset of slots with no two adjacent indices, maximizing the sum. Output the maximum sum. Greedy by largest value fai...
High-Level Design
Let best[i] be the best revenue using slots 0..i. For slot i we either skip it (best[i-1]) or take it, which forbids slot i - 1 (best[i-2] + rev[i]). So `best[i] = max(best[i-1], best[i-2] + r...