Rebuild a Trip from Unordered Boarding Passes Using Every Pass

Last updated: November 29, 2026

Quick Overview

Given a list of boarding passes (from, to) that together form one trip starting at a known airport, return an itinerary that uses every pass exactly once, choosing the lexicographically smallest one when several exist.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a list of boarding passes (from, to) that together form one trip starting at a known airport, return an itinerary that uses every pass exactly once, choosing the lexicographically smallest one when several exist.

A Google onsite question framed around reconstructing trips from Gmail flight confirmations. Greedy lexicographic DFS gets stuck in dead ends, so the interviewer is looking for recognition of an Eulerian path and Hierholzer's algorithm with a post-order trick.

What the Interviewer Expects
  • Recognizes the problem as finding an Eulerian path in a directed multigraph
  • Shows why naive greedy smallest-next fails on a dead end
  • Implements Hierholzer's algorithm with post-order appending and a final reverse
  • Keeps destinations sorted (heap or reverse-sorted list) for lexicographic order
  • Runs in O(E log E)
Key Topics to Cover
Eulerian Path
Hierholzer
DFS
Graphs
Greedy
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
  • The starting airport is unknown; how do you find it?
  • Some passes may be missing; return the minimum number of passes to add so a full trip exists.
  • Return the number of distinct valid itineraries.
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: list of [from, to] pairs (duplicates possible), starting airport.
  • A valid itinerary uses every pass once; input guarantees at least one exists.
  • Output: the lexicographically smallest va...
High-Level Design

Using each edge exactly once is an Eulerian path. Hierholzer's algorithm: from the current node, repeatedly take the smallest unused outgoing edge and go deeper; when a node has no unused edges left, ...


Submit Your Answer
Markdown supported