Quick 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.

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

  1. Trace Example 1 by hand and list which velocities are present on the table just before and just after each collision.
  2. The answer asks only when the last object leaves, not which object it is.
  3. Each exit time is a ratio of two integers; a ratio like that can be rounded up exactly with integer arithmetic alone.

Last Object Off the Table

mediumConfluentSoftware Engineer

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

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

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++.

Examples

Example 1
Input
([2, 4, 5], [2, -2, 4], 9)
Output
4
Notes
Source Example 1: the last exit is at 3.5 seconds, rounded up to 4.
Example 2
Input
([4, 1], [1, 3], 10)
Output
6
Notes
Source Example 2: same-direction catch-up; the last exit at exactly 6 seconds is returned unchanged (not 7).

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def last_fall_time(positions: list[int], velocities: list[int], table_length: int) -> int:
Sample call
last_fall_time([2, 4, 5], [2, -2, 4], 9)
Language
Case 1
Input
([2, 4, 5], [2, -2, 4], 9)
Expected
4
Case 2
Input
([4, 1], [1, 3], 10)
Expected
6

Browse More Questions

Show the approach

Approach

Key observation: a collision only redistributes velocities among the objects standing at the meeting point. Two objects that exchange velocities produce exactly the same set of (position, velocity) states as two objects that pass straight through each other, and a meeting of three or more objects also leaves with the same multiset of velocities it arrived with. So, if objects are treated as indistinguishable, the table always looks as if every object kept its own velocity and moved in a straight line. Under that view a right-moving start (p, v) reaches table_length after (table_length - p) / v seconds and a left-moving start reaches 0 after p / |v| seconds. An object leaves the real table exactly when one of these straight-line tracks reaches the end it is heading toward, and velocities are never 0 (exchanges only permute nonzero values), so every track does exit. The last object therefore leaves at the maximum of these n times. Rounding up is monotone, so the answer is the maximum over objects of ceil(distance / speed). The reference computes each ceiling with integer arithmetic (quotient plus 1 when there is a remainder), which returns whole-number times unchanged and avoids floating-point error. Which object is last, and the order of positions, do not matter. Edge cases: n = 1; all objects moving the same way; several objects leaving at the same moment; multi-object meetings; |velocity| = 10^9, where times below 1 round up to 1; distance is at least 1 because 0 < p < table_length, so the answer lies in [1, table_length - 1] and every intermediate value stays below 2^31 - 1.

Time complexity:
O(n)
Space complexity:
O(1)