← Pinterest Interview Insights

Pinterest·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Pinterest SWE interview, graph question that looked straightforward until the follow-up hit. The core problem was manageable but the cycle handling part is where things got interesting.

Questions Asked (2)

Q1

Given a list of airline tickets where each ticket is a from/to airport pair, reconstruct the full itinerary starting from JFK using every ticket exactly once. If multiple valid orderings exist, return the lexicographically smallest one.

Algorithms & Data Structures
Author's notes

I knew this was Hierholzer's algorithm territory pretty fast, Eulerian path stuff.

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 build the itinerary by adding airports to the front of the result as you backtrack.

Pro tip: Mention that this is a classic Eulerian path problem and that using a min-heap or sorted adjacency list with post-order traversal naturally yields the lexicographically smallest result. Also, clarify that the itinerary must use all tickets exactly once, so the graph is guaranteed to have an Eulerian path.

1. Understand the problem

Recognize that this is an Eulerian path problem on a directed graph where each ticket is an edge. The itinerary must start at JFK and use every edge exactly once.

2. Build the graph

Construct an adjacency list mapping each departure airport to a list of arrival airports. To facilitate lexicographical order, sort each list in reverse order or use a min-heap.

3. Apply Hierholzer's algorithm

Perform a DFS from JFK, recursively visiting destinations. After exploring all outgoing edges from an airport, add that airport to the front of the itinerary (or append and reverse at the end).

4. Handle lexicographical order

By processing destinations in sorted order (smallest first) and using post-order insertion, the resulting itinerary will be the lexicographically smallest valid sequence.

5. Analyze complexity

Explain that the time complexity is O(E log E) due to sorting, or O(E) if using a min-heap with efficient operations, where E is the number of tickets. Space complexity is O(E).

Key Points to Mention

  • Eulerian path in a directed graph
  • Hierholzer's algorithm for finding Eulerian paths
  • Lexicographical order achieved by sorting destinations and post-order traversal
  • Use of adjacency list with sorted destinations or min-heap
  • Time and space complexity analysis
  • Edge cases: multiple valid itineraries, all tickets used exactly once

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

Q2

Follow-up: how would you handle the case where the graph has cycles, meaning there are multiple repeated edges between the same pair of airports?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I stumbled.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: cycles mean multiple repeated edges between the same pair of airports, which can affect algorithms like shortest path or topological sort. Then, discuss how to handle cycles by either detecting them (e.g., DFS with recursion stack) or adapting the algorithm (e.g., using Dijkstra's with a priority queue that handles multiple edges). Finally, mention trade-offs such as time/space complexity and whether to deduplicate edges or keep them for specific use cases.

Pro tip: Show awareness that in real-world flight networks, cycles are common and often represent different flight options; instead of avoiding them, you might need to handle them efficiently by storing edges in a multigraph or using algorithms that tolerate cycles.

1. Clarify the problem and constraints

Ask whether the graph is directed or undirected, if edge weights matter, and what the goal is (e.g., shortest path, connectivity). This determines how cycles impact the solution.

2. Detect cycles if necessary

If the algorithm requires a DAG (e.g., topological sort), use DFS with a recursion stack or Union-Find to detect cycles. Mention that multiple edges between the same nodes can be treated as separate edges or deduplicated.

3. Adapt the algorithm to handle cycles

For shortest path, use Dijkstra's or Bellman-Ford which naturally handle cycles. For traversal, use visited sets to avoid infinite loops. If multiple edges exist, consider storing them in a multigraph or adjacency list with edge lists.

4. Discuss trade-offs and optimizations

Compare approaches: deduplicating edges reduces memory but may lose information; keeping multiple edges increases complexity but preserves data. Mention time/space complexity of chosen approach.

5. Summarize and relate to Pinterest context

Conclude with how this applies to Pinterest's scale (e.g., large graphs, real-time recommendations) and emphasize robustness and efficiency.

Key Points to Mention

  • Cycle detection algorithms: DFS with recursion stack, Union-Find, or Kahn's algorithm for topological sort.
  • Handling multiple edges: use a multigraph representation (e.g., adjacency list of lists) or deduplicate based on problem requirements.
  • Algorithm choices: Dijkstra's for non-negative weights, Bellman-Ford for negative weights, BFS for unweighted graphs.
  • Trade-offs: memory vs. information loss, time complexity of cycle detection vs. algorithm adaptation.
  • Real-world relevance: flight networks often have cycles and multiple routes; handling them is crucial for accurate modeling.
  • Edge cases: self-loops, parallel edges, and negative cycles (if weights allowed).

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