K Cheapest Ad Pairings From Two Sorted Price Lists
Last updated: November 29, 2026
Quick Overview
Given two ascending lists of slot prices (Feed and Stories), return the k pairs (one from each list) with the smallest combined price.
Meta
November 29, 20260
0
0 solved
Given two ascending lists of slot prices (Feed and Stories), return the k pairs (one from each list) with the smallest combined price.
Framed around bundling Feed and Stories placements in Meta's ads auction, this onsite question tests whether you can do a lazy k-way frontier expansion with a heap instead of generating all n*m pairs.
What the Interviewer Expects
- Avoid materializing all n*m pairs
- Seed the heap with (i, 0) for the first min(k, n) rows
- Expand only to (i, j + 1) after popping (i, j)
- Explain why this never misses or duplicates a pair
- State O(k log k) time
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 only the kth smallest sum, not the list.
- What if the lists are unsorted?
- How would you extend this to three lists?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Inputs:
feed_pricesandstory_prices, each sorted ascending, andk. - Output: up to
kpairs(feed, story)in non-decreasing order of sum. - If fewer than
kpairs exist, return all of the...
High-Level Design
Picture an implicit matrix where cell (i, j) is feed[i] + story[j]. Rows and columns are sorted, so each row is a sorted list and the problem is a k-way merge of the first k rows.
Seed a min-...