Coding | Round 1 | Uber | Onsite

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2022-04-25Added By Anonymous
Source: Link

Company: Uber
\nRole: software engineer i, software engineer ii
\nInterview Stage: Onsite
\nLocation: unknown
\nYear: unknown

\n

Original Post Content:

\n

I was asked the following question, does anybody know how to solve this?

\n

You a captain of a ship. Initially you are standing in a point (x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2).

\n

You know the weather forecast — the string S of length n, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side S1, the second day — S2, the n-th day — Sn and (n+1)-th day — S1 again and so on.

\n

Ship coordinates change the following way:

\n

if wind blows the direction U, then the ship moves from (x,y) to (x,y+1);
\nif wind blows the direction D, then the ship moves from (x,y) to (x,y-1);
\nif wind blows the direction L, then the ship moves from (x,y) to (x-1,y);
\nif wind blows the direction R, then the ship moves from (x,y) to (x+1,y).

\n

The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point (x,y) it will move to the point (x-1,y+1), and if it goes the direction U, then it will move to the point (x,y+2).

\n

You task is to determine the minimal number of days required for the ship to reach the point (x2,y2).

Comments