Three Ad Slot Prices Whose Total Lands Closest to the Budget
Last updated: November 29, 2026
Quick Overview
Given a list of ad slot prices and a target budget, choose three different slots whose combined price is as close as possible to the budget and return that combined price.
Meta
November 29, 20260
0
0 solved
Given a list of ad slot prices and a target budget, choose three different slots whose combined price is as close as possible to the budget and return that combined price.
A sort plus two pointer problem in an ads framing, often used as a follow-on to a pair-sum warm-up in a Meta onsite. The interviewer checks that you track the closest sum correctly and exit early on an exact match.
What the Interviewer Expects
- Sorts and fixes one price, then runs two pointers over the rest
- Updates the best sum whenever the distance to the budget shrinks
- Moves pointers based on whether the current sum is below or above the budget
- Returns immediately on an exact match
- Gives O(n^2) time and O(1) extra space after sorting
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
- Return the indices of the three slots instead of the sum.
- Find the closest sum that does not exceed the budget.
- How would you speed this up if the same prices are queried with many budgets?
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: prices, a list of at least three integers, and budget. Output: the sum of three elements at distinct indices minimizing abs(sum - budget). Ties can return either sum; we keep the first on...
High-Level Design
Sort. For each anchor i, set lo = i+1, hi = n-1. Compute the sum; if it is closer than the best so far, keep it. If the sum is below the budget, a larger value is needed, so move lo right; if ...