Last Object to Fall Off the Table
Updated
Problem statement
A straight table runs from position 0 to position tableLength. Some objects lie on it, each treated as a single point. Object i starts at positions[i] and moves at a constant velocities[i] units per second. A positive velocity means it moves right, toward tableLength. A negative velocity means it moves left, toward 0.
Whenever two objects meet at the same point, they swap velocities and carry on. This also happens when a faster object catches up with a slower one moving the same way. If three or more objects meet at one point at the same moment, they leave with the same velocities they arrived with, handed out among them in some order. The answer does not depend on that order.
An object falls off when it reaches an end of the table while moving toward that end: position 0 with a negative velocity, or position tableLength with a positive velocity. So an object that starts on an end and moves toward it falls off at time 0, and one that starts on an end and moves inward stays on the table.
Return the moment the table becomes empty, that is, the time at which the final object leaves. Round that time up to an integer; if it is already an integer, return it unchanged.
Function
lastFallTime(positions: int[], velocities: int[], tableLength: int) → intExamples
Example 1
positions = [2,4,5]velocities = [2,-2,4]tableLength = 9return = 4Objects 0 and 1 meet at position 3 after 0.5 seconds and exchange velocities. Object 1 then moves right at speed 2 and needs (9 - 3) / 2 = 3 more seconds, so it falls off at 3.5. Object 0 falls off the left end at 0.5 + 3 / 2 = 2, and object 2 falls off the right end at (9 - 5) / 4 = 1. The final object leaves at 3.5, which rounds up to 4.
Example 2
positions = [1,5]velocities = [1,-1]tableLength = 6return = 5The objects meet in the middle, at time 2, and exchange velocities. Each then needs 3 more seconds to reach an end, so both fall off at time 5. The time is already an integer, so the answer is 5.
Example 3
positions = [0,3]velocities = [4,2]tableLength = 10return = 4Object 0 starts on the left end but moves right, so it stays on the table. It catches up with object 1 at position 6 at time 1.5, and they exchange velocities. Object 1 then falls off at 1.5 + 4 / 4 = 2.5, and object 0 at 1.5 + 4 / 2 = 3.5. The answer is 4.
Constraints
1 <= positions.length <= 10^4velocities.length == positions.length1 <= tableLength <= 10^90 <= positions[i] <= tableLength, and all positions are different.1 <= |velocities[i]| <= 10^9, so no velocity is0.
More Confluent interview questions
- Sum Symmetric Pairwise RemaindersOA · Seen Nov 2026
- 0/1 KnapsackOA · Seen Nov 2026
- Count Combinations Using Weights One Through KOA · Seen Oct 2026
- Reach Target BalanceOA · Seen Mar 2026
- Tail N LinesOA · Seen Mar 2026
- Implement tail -nPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Board ValidationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Puzzle SolverPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Match a Variadic Function SignaturePHONE SCREEN · Seen Oct 2026
- Sequential Phrase Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Power-of-Two Removal OperationsIBM · OAIBM interview questions
- Macaron Order PriceOpenAI · OAOpenAI interview questions
- Daily Settlement for Long and Short PositionsBelvedere Trading · OA
Confluent interview experiences
- Confluent Older Concurrency Rounds: 12 Experiences (Part 2 of 2)
- Confluent Concurrency Interviews: 28 Experiences (Part 1 of 2)
- Confluent SWE OA, Coding and Design Interviews: 19 Experiences
- Confluent File Manipulation Round (tail -n): 19 Experiences
- Confluent Coding and System Design Questions: 33 Questions