Count Ad Bid Pairs Whose Sum Equals the Campaign Budget
Last updated: November 29, 2026
Quick Overview
Given an unsorted list of advertiser bids (integers, duplicates allowed) and a target budget, return how many index pairs i < j have bids[i] + bids[j] equal to the budget.
Meta
November 29, 20260
0
0 solved
Given an unsorted list of advertiser bids (integers, duplicates allowed) and a target budget, return how many index pairs i < j have bids[i] + bids[j] equal to the budget.
A warm-up in Meta's first coding screen, framed around ads auction data. The interviewer wants to see you move from the quadratic scan to a single hash map pass and handle duplicate values correctly, all within the first 15 minutes.
What the Interviewer Expects
- States the O(n^2) brute force and why it is too slow for millions of bids
- Uses a frequency map built while scanning so each pair is counted once
- Handles duplicates, including the case where both values are target/2
- Talks through the code on a small example before saying it is done
- Gives O(n) time and O(n) space with a reason
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 actual index pairs instead of a count. What changes?
- What if the bids are already sorted and memory is tight?
- How would you count pairs whose sum is within a tolerance of the budget?
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: bids, a list of integers (can be negative after credits), and budget. Output: the number of pairs (i, j) with i < j and bids[i] + bids[j] == budget. Pairs are by index, so [3, 3, 3]...
High-Level Design
Scan left to right and keep a dictionary seen mapping value to how many times it has appeared so far. At index j, every earlier index holding budget - bids[j] forms a valid pair with j, so add...