Time Until the Last Object Falls Off a Table After Velocity-Swapping Collisions
Company: Confluent
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Objects sit at distinct points on a table that runs from position 0 to position `table_length`. Each object moves at a constant velocity: a positive velocity moves it right, toward `table_length`, and a negative velocity moves it left, toward 0. When two objects meet, they collide and exchange velocities. An object falls off when it reaches an end of the table while moving toward that end. Given every object's starting position and velocity and the table length, return the time at which the last object falls off, rounded up to an integer.
### Function Signature
```python
def last_fall_time(positions: list[int], velocities: list[int], table_length: int) -> int:
```
### Rules
- Object `i` is at `positions[i]` at time 0 and moves at `velocities[i]` units of length per second. Objects are points, and a velocity changes only in a collision.
- When two objects are at the same point at the same moment, they exchange velocities. If three or more objects meet at one point at the same moment, they leave it with the same velocities they arrived with, reassigned so the objects keep their left-to-right order: the object that was leftmost takes the smallest velocity.
- An object leaves the table the moment it reaches position 0 while moving left or position `table_length` while moving right, and takes no part in any later collision.
- Return the smallest integer that is greater than or equal to the exact time at which the last object leaves the table. That time can be a fraction; when it is a whole number, return it unchanged.
### Constraints
- `1 <= n <= 10^5`, where `n = len(positions) = len(velocities)`
- `2 <= table_length <= 10^9`
- `0 < positions[i] < table_length`; all positions are distinct, and `positions` is not necessarily sorted
- `-10^9 <= velocities[i] <= 10^9` and `velocities[i] != 0`
- All inputs are integers. The answer is at most `table_length - 1`, so it fits in a 32-bit signed integer.
### Examples
**Example 1**
```text
Input: positions = [2, 4, 5], velocities = [2, -2, 4], table_length = 9
Output: 4
```
Objects 0 and 1 meet at position 3 after 0.5 seconds and exchange velocities. Object 2 leaves the right end after 1 second. Object 0, now moving left at speed 2 from position 3, leaves the left end at 0.5 + 1.5 = 2 seconds. Object 1, now moving right at speed 2 from position 3, leaves the right end at 0.5 + 3 = 3.5 seconds. Rounding 3.5 up gives 4.
**Example 2**
```text
Input: positions = [4, 1], velocities = [1, 3], table_length = 10
Output: 6
```
Object 1 catches object 0 at position 5.5 after 1.5 seconds, and they exchange velocities. Object 0, now at speed 3, leaves the right end at 1.5 + 1.5 = 3 seconds; object 1, now at speed 1, leaves the right end at 1.5 + 4.5 = 6 seconds.
**Example 3**
```text
Input: positions = [1, 7], velocities = [-2, -3], table_length = 8
Output: 3
```
Object 0 leaves the left end at 0.5 seconds, before object 1 can reach it. Object 1 leaves the left end at 7/3 seconds, about 2.33, which rounds up to 3.
Overview: Objects on a table move at constant velocities and exchange velocities whenever two of them meet. Given their positions, velocities and the table length, compute when the last object falls off the table, rounded up to an integer. It tests modelling collisions and edge exits, exact arithmetic on fractional times, and scaling to large inputs.
Read the full Confluent Software Engineer interview experience this question came from
Objects sit at distinct points on a table that runs from position 0 to position `table_length`. Each object moves at a constant velocity: a positive velocity moves it right, toward `table_length`, and a negative velocity moves it left, toward 0. When two objects meet, they collide and exchange velocities. An object falls off when it reaches an end of the table while moving toward that end.
Given every object's starting position and velocity and the table length, return the time at which the last object falls off, rounded up to an integer. Implement `last_fall_time(positions, velocities, table_length)`.
### Rules
- Object `i` is at `positions[i]` at time 0 and moves at `velocities[i]` units of length per second. Objects are points, and a velocity changes only in a collision.
- When two objects are at the same point at the same moment, they exchange velocities. If three or more objects meet at one point at the same moment, they leave it with the same velocities they arrived with, reassigned so the objects keep their left-to-right order: the object that was leftmost takes the smallest velocity.
- An object leaves the table the moment it reaches position 0 while moving left or position `table_length` while moving right, and takes no part in any later collision.
- Return the smallest integer that is greater than or equal to the exact time at which the last object leaves the table. That time can be a fraction; when it is a whole number, return it unchanged.
### Example 1
```text
Input: positions = [2, 4, 5], velocities = [2, -2, 4], table_length = 9
Output: 4
```
Objects 0 and 1 meet at position 3 after 0.5 seconds and exchange velocities. Object 2 leaves the right end after 1 second. Object 0, now moving left at speed 2 from position 3, leaves the left end at 0.5 + 1.5 = 2 seconds. Object 1, now moving right at speed 2 from position 3, leaves the right end at 0.5 + 3 = 3.5 seconds. Rounding 3.5 up gives 4.
### Example 2
```text
Input: positions = [4, 1], velocities = [1, 3], table_length = 10
Output: 6
```
Object 1 catches object 0 at position 5.5 after 1.5 seconds, and they exchange velocities. Object 0, now at speed 3, leaves the right end at 1.5 + 1.5 = 3 seconds; object 1, now at speed 1, leaves the right end at 1.5 + 4.5 = 6 seconds. The last exit happens at exactly 6 seconds, a whole number, so 6 is returned unchanged.
### Constraints
- `1 <= n <= 10^5`, where `n = len(positions) = len(velocities)`
- `2 <= table_length <= 10^9`
- `0 < positions[i] < table_length`; all positions are distinct, and `positions` is not necessarily sorted
- `-10^9 <= velocities[i] <= 10^9` and `velocities[i] != 0`
- All inputs are integers. The answer is at most `table_length - 1`, so it fits in a 32-bit signed integer.
No input value and no answer can exceed 2^31 - 1, so a 32-bit `int` is enough for every parameter and for the return value in Java and C++.
Constraints
- 1 <= n <= 10^5, where n = len(positions) = len(velocities)
- 2 <= table_length <= 10^9
- 0 < positions[i] < table_length; all positions are distinct, and positions is not necessarily sorted
- -10^9 <= velocities[i] <= 10^9 and velocities[i] != 0
- All inputs are integers. The answer is at most table_length - 1, so it fits in a 32-bit signed integer.
Examples
Input: ([2, 4, 5], [2, -2, 4], 9)
Expected Output: 4
Explanation: Source Example 1: the last exit is at 3.5 seconds, rounded up to 4.
Input: ([4, 1], [1, 3], 10)
Expected Output: 6
Explanation: Source Example 2: same-direction catch-up; the last exit at exactly 6 seconds is returned unchanged (not 7).
Hints
- Trace Example 1 by hand and list which velocities are present on the table just before and just after each collision.
- The answer asks only when the last object leaves, not which object it is.
- Each exit time is a ratio of two integers; a ratio like that can be rounded up exactly with integer arithmetic alone.