K Cheapest Flight and Hotel Combinations From Two Sorted Price Lists

Last updated: November 29, 2026

Quick Overview

Given two ascending lists of flight prices and hotel prices, return the k (flight, hotel) pairs with the smallest total price.

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

0

0

0 solved


Given two ascending lists of flight prices and hotel prices, return the k (flight, hotel) pairs with the smallest total price.

Framed around Google Flights and Hotels bundles, this is a common Google screen question on lazy heap expansion. The interviewer expects the candidate to avoid generating all n * m pairs and to explain the frontier that guarantees correctness.

What the Interviewer Expects
  • Avoids enumerating all pairs
  • Seeds the heap with (i, 0) for the first k flights
  • Expands only (i, j + 1) after popping (i, j)
  • States O(k log k)
  • Handles k larger than the number of pairs
Key Topics to Cover
Min-Heap
Top-K
Two Sorted Arrays
Lazy Expansion
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
  • What about three lists (flight, hotel, car)?
  • What if the lists are not sorted?
  • Stream results lazily as the user scrolls
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
  • Input: sorted lists a (flights) and b (hotels), integer k.
  • Output: up to k pairs (a[i], b[j]) with the smallest sums, in ascending order of sum.
  • If k exceeds len(a) * len(b), return all ...
High-Level Design

Think of an implicit matrix where cell (i, j) holds a[i] + b[j]. Each row is sorted left to right, so the smallest unseen cell in every row is the leftmost unseen one. Seed a min-heap with `(a[i] + ...


Submit Your Answer
Markdown supported