← Google Interview Insights

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

Intermediate
Jun 2026

Summary

Google SWE coding round with a grid path-counting problem. Pretty standard dynamic programming territory but the diagonal move twist made it less obvious than I expected.

Questions Asked (1)

Q1

Given an m x n 2D matrix, how many unique paths exist from the bottom-left corner to the top-right corner if you can only move up, right, or diagonally up-right? Implement the solution and provide test cases.

Algorithms & Data Structures
Author's notes

The diagonal move is what got me initially.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the movement directions and coordinate system, then model the problem as a dynamic programming (DP) problem where dp[i][j] represents the number of ways to reach cell (i, j) from the start. Derive the recurrence dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1] (with appropriate boundary conditions), implement it with O(mn) time and O(n) space, and test with small cases and edge cases.

Pro tip: Mention that this is a generalization of the classic unique paths problem and that the DP can be optimized to 1D space; also discuss how to handle large numbers (e.g., modulo arithmetic) if needed.

1. Clarify the problem

Confirm the matrix dimensions, the starting and ending cells, and the allowed moves (up, right, diagonally up-right). Ensure you understand the coordinate system and whether the matrix is 0-indexed or 1-indexed.

2. Define the DP state and recurrence

Let dp[i][j] be the number of ways to reach cell (i, j) from the start. Derive the recurrence: dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1], with base case dp[start] = 1 and out-of-bounds cells contributing 0.

3. Implement the solution

Write code to compute the DP table, either using a 2D array or optimizing to 1D space. Handle edge cases such as a 1x1 matrix or when start equals end.

4. Test with cases

Provide test cases: small matrices (e.g., 2x2, 3x3), edge cases (1xN, Nx1), and a larger case to verify performance. Compare with brute force for small inputs if possible.

5. Analyze complexity

State the time complexity O(mn) and space complexity O(n) if optimized. Discuss potential improvements or variations (e.g., modulo arithmetic for large results).

Key Points to Mention

  • Dynamic programming approach with recurrence relation
  • Base case and boundary conditions
  • Space optimization from O(mn) to O(n)
  • Time and space complexity analysis
  • Handling large numbers (e.g., modulo 10^9+7) if required
  • Test cases including edge cases and brute-force verification

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