Resolve Collisions Between Totes Moving on a Two-Way Conveyor
Last updated: November 29, 2026
Quick Overview
Totes sit on a straight conveyor, each with a signed size: positive moves right, negative moves left, all at the same speed. When two totes meet the smaller one is diverted off the belt, equal sizes are both diverted, and the larger continues. Return the totes remaining after all collisions, in order.
Amazon
November 29, 20260
0
0 solved
Totes sit on a straight conveyor, each with a signed size: positive moves right, negative moves left, all at the same speed. When two totes meet the smaller one is diverted off the belt, equal sizes are both diverted, and the larger continues. Return the totes remaining after all collisions, in order.
An Amazon onsite simulation question that is really a stack. The interviewer wants you to see that only a left-mover meeting earlier right-movers can collide, and to handle the chain reaction with a while loop.
What the Interviewer Expects
- Identifies that only right-then-left pairs collide
- Uses a stack of survivors
- Loops while the incoming left-mover beats the top right-mover
- Handles equal sizes destroying both
- Argues O(n) amortized time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Also return the time at which each collision happens.
- What if totes have different speeds?
- What if the conveyor is circular?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of nonzero integers. Output: list of survivors left to right. Totes moving apart or in the same direction never meet....
High-Level Design
Scan left to right with a stack of survivors so far. A right-mover is pushed, since it can only collide with something that arrives later. A left-mover can collide with right-movers on top of the stac...