Uber on-site question - Rejected!
Anonymous User
843

There is a grid. O represents a Robot, X represents obstacle, E represents empty space where you can move. Borders are also considered as an obstacle.

[O E E E E] 
[E O X E E] 
[X E X X E] 
[E E O X E] 
[X E X E E] 

Query:

[2, 2, 4, 1]

Output: Robot at (1,1). List<int[]> is return type. Here, output will be [ {1,1} ].
Explanation: Robot at (1,1) has distance of 2 on left, 2 on top, 4 on bottom and 1 on right.

Edit-1 (detailed explanation on query) : How is distance calculated? The robot at (1,1) has distance of 2 on left to border. E and then the border, so distance of 2. Similarly, on the top E and then a border so distance of 2. On bottom, 3 E + border (out of bound) = 4. On right, obstacle X at distance of 1.
So, the query is [2,2,4,1] representing distance to obstacle on left, top, bottom, right. For this query, robot at index (1,1) satisfies the query so return that robot. There might be many robots in grid that satisfy the query or even none robot that satisfies this query

There can even be multiple robots satisfying this query. Return all the robots that satisfy the query.

Consider this as a read heavy system. So, there will be lot of queries. Focus should be on pre-computing efficiently.

I could not come up with an optimized approach. I mentioned to go in 4 directions from Robot and store it in Map but apparently that is not optimized. Interviewer hinted I should do memoization but I have no idea how will even memorize directions or anything.

Comments (7)