Given a pair of x, y coordinates, find the shortest path from origin to it and avoiding obstacles.
Anonymous User
1967

I couldn't find something similar to this on LC so I decided to post this.

This was in an interview I had at a faang company (won't name it but not hard to figure out).

Given a 2D array as a map and a pair of x y coordinates, find the shortest distance from the origin 0,0 to the point x,y. There may be obstacles along the way. At any point (x, y), the element is False if there is no obstacle and True if there is.

Test case 1:

input: 
map = [
[False, False, False], 
[False, False, False], 
[False, False, False]
], x = 2, y = 2. 

output: 
4 

Explanation: 
path is 0,0 -> 0,1 -> 0,2 -> 1,2 -> 2,2

Test case 2:

input:
map = [
[False, False, False], 
[True, True, False], 
[False, False, False]
], x = 2, y = 0

output: 6

Explanation: 
path is 0,0 -> 0,1 -> 0,2 -> 1,2 -> 2,2 -> 2,1 -> 2,0

Test case 3:

input:
map = [
[False, False, False], 
[False, True, False], 
[False, False, False]
], x = 2, y = 0

output: 2

Explanation: 
path is 0,0 -> 1,0 -> 2,0

I thought this was similar to this unique paths question, so I wrote a dfs just for two directions and only realized after the interview that it's wrong and I'd have to go about it.

Comments (3)