← Pinterest Interview Insights

Pinterest·Machine Learning Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026Remote

Summary

Pinterest MLE interview, two parts both centered on the same graph problem. Nothing behavioral, just algorithms the whole time. Left feeling okay about part one and a bit shaky on the follow-up.

Questions Asked (2)

Q1

You have a list of bus routes where each route is a loop of stops. Given a source stop and a target stop, return the minimum number of buses needed to travel from source to target, or -1 if it's not possible.

Algorithms & Data Structures
Author's notes

Went with BFS treating each route as a node rather than each stop.

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 routes to reach any route containing the target stop. If the source and target are the same stop, return 0; if no path exists, return -1.

Pro tip: Clarify edge cases upfront: if source equals target, answer is 0; if either stop is not in any route, answer is -1. Also, mention that BFS on routes (not stops) is more efficient because it reduces the graph size and naturally counts the number of buses.

1. Clarify and handle edge cases

Confirm with the interviewer that source and target are stops, and that a bus route is a loop. Handle trivial cases: if source == target, return 0; if either stop is not in any route, return -1.

2. Build route-stop mapping

Create a mapping from each stop to the list of routes that contain it. This allows quick lookup of which routes serve a given stop.

3. Construct route graph

For each stop, connect all routes that share that stop by adding edges between them. This forms a graph where nodes are routes and edges represent a transfer between routes at a common stop.

4. BFS from source routes

Initialize a queue with all routes containing the source stop, marking them as visited with distance 1. Perform BFS: for each route, explore its neighbors (connected routes), incrementing distance. If a route containing the target stop is reached, return the distance.

5. Return result or -1

If BFS completes without reaching any route that contains the target stop, return -1. Otherwise, the first time we reach such a route, return the current distance.

Key Points to Mention

  • Graph modeling: routes as nodes, edges between routes sharing a stop.
  • BFS guarantees minimum number of buses because each edge represents one additional bus.
  • Time complexity: O(N * S) where N is number of routes and S is average stops per route, due to building the graph.
  • Space complexity: O(N^2) in worst case for the route graph, but can be optimized by not explicitly building all edges.
  • Optimization: Instead of building full route graph, during BFS, for each route, iterate through its stops and enqueue all unvisited routes that contain those stops.
  • Edge cases: source == target (return 0), unreachable target (return -1), stops not in any route.

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

Q2

Follow-up: given the same bus routes setup, now support repeated queries asking for the minimum number of buses between any two arbitrary stops. How do you handle this efficiently?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I got a little lost.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that this is a shortest path problem on a graph where stops are nodes and bus routes form edges (or hyperedges). Precompute all-pairs shortest paths using BFS from each stop or Floyd-Warshall if the graph is small, then answer each query in O(1) time. Discuss trade-offs between preprocessing time, memory, and query latency.

Pro tip: Mention that if the number of stops is large, you can use a meet-in-the-middle BFS or bidirectional BFS for each query, but for repeated queries, precomputation is key. Also, consider that bus routes may have multiple stops, so model each route as a clique or use a bipartite graph with route nodes to avoid O(k^2) edges per route.

1. Model the problem as a graph

Represent stops as nodes and bus routes as connections. Decide whether to use a simple graph (each route connects all its stops pairwise) or a bipartite graph with route nodes to reduce edge count.

2. Choose a precomputation strategy

For repeated queries, precompute shortest paths between all pairs of stops. Use BFS from each stop if the graph is unweighted (each bus ride counts as 1), or Floyd-Warshall if the number of stops is small (e.g., <= 500).

3. Optimize for memory and time

If the number of stops is large, consider storing distances in a 2D array if memory allows, or use a more compact representation. Discuss trade-offs: O(V*(V+E)) time for BFS from each node vs O(V^3) for Floyd-Warshall.

4. Answer queries in O(1)

After precomputation, each query is simply a lookup in the precomputed distance matrix. If no path exists, return -1 or infinity.

5. Discuss scalability and alternatives

If the graph is too large for all-pairs precomputation, consider using landmarks, contraction hierarchies, or caching frequent queries. Also, mention that if queries are not known in advance, online algorithms like bidirectional BFS may be better.

Key Points to Mention

  • Graph modeling: stops as nodes, routes as edges or hyperedges; bipartite graph to avoid O(k^2) edges per route.
  • BFS for unweighted shortest paths (each bus ride = 1).
  • All-pairs shortest paths: BFS from each node (O(V*(V+E))) or Floyd-Warshall (O(V^3)).
  • Trade-offs: preprocessing time vs query time vs memory.
  • Handling disconnected components: return -1 or infinity.
  • Scalability: when all-pairs is infeasible, use bidirectional BFS, landmarks, or caching.

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