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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

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
min-heap
k-way merge
sorted matrix frontier
top-k
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Inputs: feed_prices and story_prices, each sorted ascending, and k.
  • Output: up to k pairs (feed, story) in non-decreasing order of sum.
  • If fewer than k pairs 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-...


Submit Your Answer
Markdown supported