Google | Phone | Unique Paths to Cell
Anonymous User
1720

I had a phone screen with a google engineer and received the following question with slight changes to the requirements:
https://leetcode.com/problems/unique-paths/

The requirement differences are the following:

  1. You start at the bottom left and need to go to the top right
  2. Can go up, right, or diagonal (up and right)

I was able to come up with a DFS solution with memoization in 20 minutes (tweaked it to match the linked leetcode problem):

def uniquePaths(self, m: int, n: int) -> int:
        ROW = m 
        COL = n 
        memo = [[-1]*COL for _row in range(ROW)]
        
        def dfs(row, col):
            if row < 0 or row >= ROW or col < 0 or col >= COL:
                return 0
            if row == ROW-1 and col == COL-1:
                return 1
            
            if memo[row][col] != -1:
                return memo[row][col]
            
            result = sum(dfs(row+1, col), dfs(row, col+1) )
            memo[row][col] = result
            return result
            
        return dfs(0, 0)

The follow up question was

  1. What if we were given a coordinate and the robot MUST pass through it before reaching the top right. How many combinations are there then?
  2. What if we're given a list of coordinates that the robot needs to pass through?

The follow up stumped me and at the time I couldn't come up with a proper solution. How to do it came to me while showering after the interview and now I'm banging my head against the wall for not realizing it sooner. The interview anxiety was real.

The results ended up being a rejection and to try again in 9 months.

Comments (6)