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:
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
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.