Problem · Math
Last Truck to Leave the Lane
Learn this problemProblem 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[]) → doubleExamples
Example 1
laneLength = 10initial = [2,6]velocity = [2,-1]return = 6.0The 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
- Chain of CommandOA · Seen Jul 2026
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026
- Maximal Square AreaONSITE INTERVIEW · Seen May 2026