← Google Interview Insights

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

SeniorPrefer not to say
Jun 2026

Summary

Google onsite coding round, second interview of the day. The problem was a grid pathfinding variant where you minimize the maximum cell value on a path rather than the sum, with a follow-up that pushed into general weighted graphs. Harder than it looks if you haven't thought carefully about why Dijkstra even works when the cost function isn't additive.

Questions Asked (3)

Q1

Given a grid of integer heights, find a path from the top-left to the bottom-right corner such that the maximum cell value along the path is minimized. You can move to any of the four adjacent cells.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew this was Dijkstra-flavored but fumbled the transition from 'minimize sum' to 'minimize max' for a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and constraints, then propose a binary search on the answer combined with BFS/DFS to check feasibility, or a Dijkstra-like algorithm using a priority queue that minimizes the maximum cell value along the path. Discuss trade-offs between approaches and analyze time/space complexity.

Pro tip: Mention that this is a minimax path problem and can be solved with a modified Dijkstra where the priority is the maximum value so far, or with binary search + BFS; also note that if the grid is small, a simpler BFS with a threshold might suffice, but for large grids, the priority queue approach is more efficient.

1. Clarify the problem and constraints

Ask about grid size, value ranges, and whether diagonal moves are allowed. Confirm that the path must be simple (no cycles) and that we want to minimize the maximum cell value.

2. Discuss possible approaches

Propose two main strategies: (1) binary search on the answer with BFS/DFS to check if a path exists with all cells ≤ threshold, and (2) a modified Dijkstra where the cost is the maximum cell value along the path.

3. Analyze time and space complexity

For binary search + BFS: O(N log(maxVal)) time, O(N) space. For Dijkstra: O(N log N) time, O(N) space. Compare and choose based on constraints.

4. Implement the chosen approach

Write clean code for the selected algorithm, handling edge cases like single cell, unreachable path (though always reachable in grid), and large values.

5. Test with examples and edge cases

Walk through a small example, test with increasing values, and verify correctness. Discuss potential optimizations like early termination.

Key Points to Mention

  • Binary search on the answer with BFS/DFS feasibility check
  • Modified Dijkstra with priority queue minimizing maximum value
  • Time and space complexity analysis for both approaches
  • Trade-offs between the two approaches (e.g., binary search may be simpler, Dijkstra may be faster for certain inputs)
  • Handling of edge cases (e.g., 1x1 grid, all equal values)
  • Proof of correctness: why the algorithm finds the optimal path

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

Q2

Follow-up: given a directed weighted graph and a source node, return the maximum shortest-path distance across all nodes, or -1 if any node is unreachable.

Algorithms & Data Structures
Author's notes

This is basically standard Dijkstra with one extra line at the end.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a single-source shortest path problem on a directed weighted graph with non-negative weights, so use Dijkstra's algorithm. After computing distances, check if any node is unreachable (distance = infinity); if so, return -1. Otherwise, return the maximum distance among all nodes.

Pro tip: Clarify edge weight assumptions upfront—if negative weights are possible, Dijkstra fails and Bellman-Ford is needed. Also, mention that the maximum shortest-path distance is the graph's eccentricity of the source, which is a useful term to show depth.

1. Clarify requirements and constraints

Ask about edge weights (negative? zero?), graph size, and whether the graph is connected. This determines the algorithm choice and edge cases.

2. Choose the right algorithm

For non-negative weights, Dijkstra with a priority queue is optimal (O((V+E) log V)). If negative weights exist, use Bellman-Ford and detect negative cycles.

3. Compute shortest paths from source

Run the algorithm to get distances to all nodes. Track visited nodes and update distances efficiently.

4. Check reachability and find maximum

Iterate through all distances: if any is infinity, return -1. Otherwise, return the maximum finite distance.

5. Analyze complexity and edge cases

State time and space complexity. Discuss edge cases: source isolated, graph with one node, zero-weight edges, and large graphs.

Key Points to Mention

  • Dijkstra's algorithm for non-negative weights, with a priority queue for efficiency
  • Bellman-Ford as an alternative if negative weights are allowed, including negative cycle detection
  • Handling unreachable nodes by checking for infinity distances
  • Time complexity: O((V+E) log V) for Dijkstra with a binary heap
  • Space complexity: O(V+E) for adjacency list and distance array
  • Edge cases: empty graph, source with no outgoing edges, all nodes reachable

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

Q3

Why does Dijkstra's algorithm remain correct when the path cost is defined as the maximum edge or node value encountered, rather than the sum of weights?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This was the part that actually stressed me out.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that Dijkstra's correctness relies on the optimal substructure and monotonicity of the path cost function. Show that the max function is monotonic non-decreasing along a path, so the greedy selection of the minimum tentative cost remains valid. Then contrast with sum to highlight the shared property.

Pro tip: Mention that this is a special case of Dijkstra on a semiring where the 'addition' is max and 'multiplication' is identity, and that the algorithm works for any monotonic, isotonic cost function. This demonstrates deep understanding beyond rote memorization.

1. Restate the problem and define the cost function

Clarify that the path cost is the maximum edge or node value along the path, not the sum. Define the cost of a path as max(weights) and note that we seek the path minimizing this maximum.

2. Identify the key properties for Dijkstra's correctness

Dijkstra requires that extending a path cannot decrease its cost (monotonicity) and that the optimal substructure holds: any subpath of an optimal path is optimal. Show that max satisfies these.

3. Prove monotonicity and optimal substructure for max

For any path P and extension P+e, cost(P+e) = max(cost(P), w(e)) >= cost(P). Also, if P is optimal for max, any subpath is optimal, because if a cheaper subpath existed, replacing it would yield a lower max for the whole path.

4. Explain why the greedy selection remains valid

Since costs never decrease as paths grow, the node with the smallest tentative max-cost cannot be improved by any future extension. Thus, when extracted, its cost is final, just as in the sum version.

5. Conclude and mention generalization

Summarize that Dijkstra works for any cost function that is monotonic and isotonic (order-preserving). The max function is one such example, often called the 'bottleneck' path problem.

Key Points to Mention

  • Monotonicity: extending a path cannot decrease the max cost.
  • Optimal substructure: subpaths of optimal paths are optimal.
  • Greedy selection: the minimum tentative cost node is finalized when extracted.
  • Contrast with sum: both sum and max are monotonic and isotonic.
  • Generalization: Dijkstra works on any semiring with appropriate properties.
  • Real-world analogy: bottleneck path problem (e.g., maximizing load capacity).

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