← Pinterest Interview Insights

Pinterest·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Got a graph/DFS problem at Pinterest that looks straightforward until you realize the lexical ordering constraint changes everything about how you structure your solution.

Questions Asked (1)

Q1

Given a list of airline tickets as departure/arrival airport pairs, reconstruct the full itinerary starting from JFK. If multiple valid itineraries exist, return the one that comes first lexicographically when read as a single string.

Algorithms & Data Structures
Author's notes

I knew it was a graph problem right away and jumped to DFS, which was the right instinct.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the tickets as a directed graph and use Hierholzer's algorithm to find an Eulerian path starting from JFK. To ensure the lexicographically smallest itinerary, sort each airport's destinations in reverse order and use a stack-based DFS, building the itinerary in reverse.

Pro tip: Clarify that the problem guarantees a valid itinerary exists; if not, discuss handling edge cases. Also, mention that using a min-heap for destinations can achieve the same lexicographic order, but sorting once is more efficient.

1. Understand the problem

Recognize that the tickets form a directed graph where each airport is a node and each ticket is a directed edge. The goal is to find an Eulerian path starting from JFK that uses all edges exactly once, and among all such paths, return the lexicographically smallest one.

2. Choose the algorithm

Use Hierholzer's algorithm to find an Eulerian path. This algorithm is efficient (O(E log E) due to sorting) and naturally handles the requirement to use all edges.

3. Ensure lexicographic order

Sort the destinations for each airport in reverse lexicographic order (or use a min-heap) so that when we explore, we visit the smallest lexical destination first. This ensures the final itinerary is lexicographically smallest.

4. Implement DFS with stack

Perform an iterative DFS using a stack, starting from JFK. At each step, pop the next destination from the current airport's list (which is sorted in reverse) and push it onto the stack. When no destinations remain, add the airport to the itinerary. Finally, reverse the itinerary to get the correct order.

5. Analyze complexity and edge cases

Discuss time complexity: O(E log E) due to sorting, where E is the number of tickets. Space complexity: O(E). Mention edge cases like multiple tickets between same airports, and the guarantee of a valid itinerary.

Key Points to Mention

  • Graph representation: adjacency list with sorted destinations
  • Eulerian path concept and Hierholzer's algorithm
  • Lexicographic ordering: sorting destinations in reverse or using a min-heap
  • Iterative DFS to avoid recursion depth issues
  • Time and space complexity analysis
  • Handling of duplicate tickets and ensuring all edges are used

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.