← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Google SWE coding round, one graph problem that looked straightforward until the follow-ups started piling up. Spent most of the time on the counting logic rather than the traversal itself, which I didn't fully anticipate.

Questions Asked (3)

Q1

Given an undirected graph and two nodes s and t, find the number of distinct shortest paths between them. Be ready to handle both unweighted and weighted edges.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I got the BFS part down pretty fast, the real stumble was when they pushed toward weighted edges and Dijkstra.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS for unweighted graphs to compute distances from s and count shortest paths by summing counts from predecessors. For weighted graphs, use Dijkstra's algorithm with a modified relaxation step that accumulates path counts when equal distances are found. Clearly state assumptions (e.g., non-negative weights) and discuss handling of zero-weight edges or negative cycles if applicable.

Pro tip: Mention that path counts can grow exponentially, so use modular arithmetic or big integers, and clarify whether paths are considered distinct by node sequence or edge sequence. Also, note that for weighted graphs with zero-weight edges, Dijkstra may need a tweak to avoid infinite loops.

1. Clarify the problem

Ask about graph properties: directed/undirected, weighted/unweighted, presence of negative weights, and whether paths are simple. Confirm if the count should be modulo a number.

2. Choose the right algorithm

For unweighted graphs, BFS is optimal. For weighted graphs with non-negative weights, Dijkstra's algorithm is suitable. If negative weights exist, Bellman-Ford or Floyd-Warshall may be needed.

3. Modify to count paths

During traversal, maintain an array of distances and an array of path counts. When relaxing an edge, if a shorter distance is found, update distance and set count to predecessor's count; if equal distance, add predecessor's count.

4. Handle edge cases and complexity

Discuss handling of zero-weight edges, disconnected nodes, and large counts. Analyze time and space complexity: O(V+E) for BFS, O((V+E) log V) for Dijkstra.

5. Test and validate

Walk through a small example to verify correctness. Consider edge cases like multiple shortest paths, no path, or s == t.

Key Points to Mention

  • BFS for unweighted graphs: level-order traversal ensures shortest paths.
  • Dijkstra's algorithm for weighted graphs with non-negative weights.
  • Path count accumulation: sum counts from all predecessors that yield the shortest distance.
  • Modular arithmetic to prevent integer overflow for large path counts.
  • Time and space complexity analysis for both approaches.
  • Handling of zero-weight edges and negative cycles (if applicable).

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

Q2

What are the time and space complexities for the BFS-based and Dijkstra-based versions of this shortest path counting problem?

Algorithms & Data Structures
Author's notes

Answered fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: counting all shortest paths from a source to all nodes or a target. Then, analyze BFS for unweighted graphs and Dijkstra for weighted graphs, deriving time and space complexities based on the data structures used. Compare and contrast the two approaches, noting when each is appropriate.

Pro tip: Mention that for Dijkstra, using a Fibonacci heap can improve time complexity to O(E + V log V), but in practice binary heaps are often preferred due to lower constant factors. Also, note that the space complexity for both is O(V + E) to store the graph and auxiliary arrays.

1. Clarify the problem

Confirm that the problem involves counting the number of shortest paths from a source to all other nodes (or a specific target) in a graph. Specify whether the graph is unweighted (BFS) or weighted with non-negative weights (Dijkstra).

2. Analyze BFS approach

For unweighted graphs, BFS explores nodes level by level. Time complexity is O(V + E) with adjacency list, and space complexity is O(V) for the queue and distance/path count arrays.

3. Analyze Dijkstra approach

For weighted graphs, Dijkstra with a binary heap runs in O((V + E) log V) time. Space complexity is O(V + E) for the graph and O(V) for auxiliary arrays. Mention that using a Fibonacci heap gives O(E + V log V) time.

4. Compare and contrast

Highlight that BFS is a special case of Dijkstra for unweighted graphs, offering better time complexity. Discuss trade-offs: BFS is simpler and faster for unweighted graphs, while Dijkstra handles weights but with higher time complexity.

5. Summarize complexities

State the final time and space complexities clearly: BFS: O(V+E) time, O(V) space; Dijkstra (binary heap): O((V+E) log V) time, O(V+E) space. Note that path counting adds only constant overhead per node.

Key Points to Mention

  • BFS time complexity: O(V + E) for unweighted graphs using adjacency list.
  • Dijkstra time complexity: O((V + E) log V) with binary heap; O(E + V log V) with Fibonacci heap.
  • Space complexity for both: O(V + E) to store the graph, plus O(V) for distance and path count arrays.
  • Path counting requires maintaining an array of counts and updating it when a shorter or equal-length path is found.
  • BFS is optimal for unweighted graphs; Dijkstra is needed for weighted graphs with non-negative weights.
  • In practice, binary heaps are often used for Dijkstra due to lower constant factors despite worse asymptotic complexity compared to Fibonacci heaps.

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

Q3

How does your solution handle edge cases like no path existing between s and t, or graphs with cycles?

Algorithms & Data Structures
Author's notes

Cycles tripped me up for a second because I panicked thinking they'd cause infinite loops, but BFS visited-node tracking handles that naturally.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem context and the specific algorithm being used, then systematically address each edge case: no path, cycles, and any other relevant cases. Explain how your solution detects and handles these cases, emphasizing correctness and efficiency.

Pro tip: Demonstrate awareness of trade-offs: for example, in a DFS-based solution, cycles are handled via visited states, but recursion depth might be an issue; mention iterative alternatives or cycle detection techniques like coloring. Also, discuss how you would test these edge cases.

1. Clarify the problem and algorithm

Restate the problem and specify the algorithm (e.g., BFS, DFS, Dijkstra) and data structures used. This sets the context for edge case handling.

2. Address no path existing

Explain how your algorithm detects that no path exists (e.g., BFS/DFS exhausts reachable nodes without finding t) and what it returns (e.g., null, empty list, or a specific value).

3. Address cycles in the graph

Describe how your algorithm avoids infinite loops in cyclic graphs, such as using a visited set or marking nodes as processed.

4. Discuss other edge cases

Mention additional edge cases like s == t, disconnected graphs, self-loops, or negative weights (if applicable) and how they are handled.

5. Summarize correctness and complexity

Conclude by reiterating that the solution correctly handles these cases without compromising time/space complexity, and mention testing strategies.

Key Points to Mention

  • Use of visited set or boolean array to prevent revisiting nodes in cyclic graphs.
  • Termination condition for no path: when the search queue/stack is empty or all reachable nodes are visited.
  • Return value or exception for no path (e.g., null, empty list, or -1).
  • Handling of s == t (trivial path) and disconnected components.
  • Cycle detection techniques (e.g., DFS with recursion stack, Union-Find for undirected graphs).
  • Time and space complexity remains O(V+E) for BFS/DFS even with edge cases.

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