Return the K Cheapest Product and Shipping Option Combinations

Last updated: November 29, 2026

Quick Overview

Given a sorted list of product prices and a sorted list of shipping prices, return the k pairs (one product, one shipping option) with the smallest combined cost, in increasing order of total.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a sorted list of product prices and a sorted list of shipping prices, return the k pairs (one product, one shipping option) with the smallest combined cost, in increasing order of total.

An Amazon technical screen heap question tied to checkout, where a page shows the cheapest product plus delivery combinations. Interviewers want to see lazy expansion from a heap rather than generating all n times m pairs.

What the Interviewer Expects
  • Reject materializing all pairs when lists are large
  • Seed the heap with (a[i] + b[0]) for the first min(k, n) products
  • On each pop, push the same product with the next shipping option
  • Explain why this frontier never misses a smaller pair
  • Give O(k log k) time
Key Topics to Cover
heaps
k-way merge
top-k
sorted arrays
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
  • How would you find only the kth smallest sum without listing pairs?
  • What if the lists were unsorted?
  • How would you extend this to three lists (product, shipping, gift wrap)?
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: sorted integer lists a and b (possibly with duplicates) and k >= 1. Output: up to k pairs [a[i], b[j]] in non-decreasing order of a[i] + b[j]. If fewer than k pairs exist, return all of them....

High-Level Design

Think of an n by m grid where cell (i, j) holds a[i] + b[j]; rows and columns are sorted. The smallest unvisited cell of row i is always the next column after the last popped one. So start the heap wi...


Submit Your Answer
Markdown supported