K Cheapest Phone and Accessory Combo Bundles

Last updated: November 29, 2026

Quick Overview

Given two price lists sorted ascending, one for phones and one for accessories, return the K cheapest (phone, accessory) bundles by total price without generating every combination.

Flipkart
Coding & Algorithms
Software Engineer
Flipkart
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given two price lists sorted ascending, one for phones and one for accessories, return the K cheapest (phone, accessory) bundles by total price without generating every combination.

A Flipkart onsite heap question built around combo offers on the product page ('frequently bought together'). The interviewer checks that you avoid the n*m blowup and use a min heap frontier over the implicit sorted matrix of sums.

What the Interviewer Expects
  • Notice that all n*m sums form a matrix sorted along rows and columns
  • Seed a min heap with the first accessory for each of the first K phones
  • Push the next accessory for a phone each time its bundle is popped
  • State O(K log K) time
  • Handle K larger than n*m and empty lists
Key Topics to Cover
heaps
top-k
k-way merge
sorted matrix
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 cheapest total without listing all K?
  • What if there are three lists (phone, case, charger)?
  • How would you dedupe bundles with the same total price for display?
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: phones (length n) and accessories (length m), both sorted ascending; integer K.
  • Output: up to K pairs in nondecreasing order of total price.
  • n and m can be 10^4 each, so n*m = 10...
High-Level Design

Picture the sums as a grid where row i is a phone and column j an accessory. Every row is sorted and every column is sorted. The cheapest unseen bundle is always the next element of some row. So r...


Submit Your Answer
Markdown supported