Problem · Math

Last Truck to Leave the Lane

Learn this problem
MediumUberINTERNOA
See Uber hiring insights

Problem statement

A lane is represented by the line segment from 0 to laneLength. Several trucks begin at distinct coordinates on the lane.

initial[i] is the starting coordinate of truck i, and velocity[i] is its signed velocity. A positive velocity moves a truck to the right, while a negative velocity moves it to the left.

When a truck crosses either boundary, it leaves the lane. When two trucks collide, they exchange their velocities and directions.

Return the time at which the last truck leaves the lane.

Function

lastTruckExitTime(laneLength: int, initial: int[], velocity: int[]) → double

Examples

Example 1

laneLength = 10initial = [2,6]velocity = [2,-1]return = 6.0

The trucks collide after 4/3 time units and exchange velocities. One truck then leaves through the right boundary at time 4, while the other leaves through the left boundary at time 6. Therefore, the last truck leaves at time 6.0.

More Uber problems

drafts saved locally
public double lastTruckExitTime(int laneLength, int[] initial, int[] velocity) {
  // write your code here
}
laneLength10
initial[2,6]
velocity[2,-1]
expected6.0
checking account