← Pinterest Interview Insights

Pinterest·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Pinterest ML Engineer interview with a hard graph/BFS problem. The question was algorithmic and felt more like a software engineering screen than anything ML-specific, which threw me off a bit.

Questions Asked (1)

Q1

Given a list of bus routes where each bus cycles through its stops indefinitely, find the minimum number of buses you need to take to get from a source stop to a target stop. Return -1 if it's impossible.

Algorithms & Data Structures
Author's notes

The instinct to BFS over stops instead of routes will burn you here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where each bus route is a node, and edges connect routes that share at least one stop. Then perform BFS from all routes containing the source stop to find the minimum number of buses to reach any route containing the target stop. If source and target are the same stop, return 0; if no path exists, return -1.

Pro tip: Clarify with the interviewer whether the source and target stops are guaranteed to be in the routes, and discuss trade-offs between building the route graph upfront versus on-the-fly to optimize for memory or time.

1. Clarify problem constraints and edge cases

Ask about input size, whether source and target can be the same, and if stops are guaranteed to exist in the routes. This helps determine the optimal approach and avoid misunderstandings.

2. Model as a graph of bus routes

Treat each bus route as a node. Connect two routes with an edge if they share at least one stop. This transforms the problem into finding the shortest path in an unweighted graph.

3. Build the graph efficiently

Use a hash map to map each stop to the list of routes that contain it. Then, for each stop, connect all routes in its list to each other, avoiding duplicate edges.

4. Run BFS from source routes to target routes

Initialize a queue with all routes containing the source stop, marking them as visited with distance 1. BFS level by level until a route containing the target stop is found, returning the distance.

5. Handle edge cases and return result

If source equals target, return 0. If BFS exhausts without reaching a target route, return -1. Otherwise, return the minimum number of buses (BFS distance).

Key Points to Mention

  • Graph modeling: routes as nodes, shared stops as edges.
  • BFS for shortest path in unweighted graph.
  • Hash map to efficiently map stops to routes.
  • Time complexity: O(N*S + R^2) where N is number of routes, S is average stops per route, and R is max routes per stop.
  • Space complexity: O(N + total stops) for graph and visited set.
  • Edge cases: source == target, no common routes, disconnected components.

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