Google | L5 | round 1 | India
Company: Google
Role: L5
Interview Stage: Phone Screen
Location: India
Year: unknown
Original Post Content:
I gave one round of interview at Google last week. I'm not sure what's happening at Google because they are asking crazy hard questions to people.
You are given an NxM maze where each cell is either empty or a wall. There is one cell which is the exit from the maze. You are tasked to find a set of universal instructions like DDRLU which, applied from any position, will make you exit from the maze.
After getting some hints from the interviewer, the solution I found below:
Track current_instruction.
For each empty cell (i, j) in the grid, apply current instruction. If you reach the exit then it's fine, otherwise let's say you reach cell x, y.
Update current_instruction = current_instruction + path from x, y to exit
You will do this for each cell.
You can prove that this works by induction.
Not sure about time complexity though. Seems N3M3 to me since we are iterating over each point and the length of current_instruction could be N2M2 length.