Simulate Head-On Collisions of Warehouse Robots on One Rail
Last updated: November 29, 2026
Quick Overview
Robots on a single rail are given in left-to-right order as signed integers: magnitude is battery size, sign is direction (positive moves right); when two meet head-on the smaller is destroyed, equal ones both are, and you must return the surviving robots in order.
0
0
0 solved
Robots on a single rail are given in left-to-right order as signed integers: magnitude is battery size, sign is direction (positive moves right); when two meet head-on the smaller is destroyed, equal ones both are, and you must return the surviving robots in order.
A stack simulation question in the style Google uses to test invariant reasoning, framed around robotics in a fulfillment setting. The interviewer wants to see you identify that only a right-mover followed by a left-mover can collide and that a stack captures all pending right-movers.
What the Interviewer Expects
- Identifies the only colliding configuration: right-mover on the left, left-mover on the right
- Uses a stack of survivors so far
- Lets one left-mover destroy several right-movers in a loop
- Handles equal-size mutual destruction
- Argues O(n) since each robot is pushed and popped at most once
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
- Robots have different speeds; how do you find the order of collisions?
- Return the time each robot is destroyed, given unit speed and positions.
- Robots can also move in two dimensions on a grid; what changes?
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 in rail order.
- Same speed for all robots; only head-on meetings matter (same-direction robots never meet).
- Output: survivors in original order....
High-Level Design
Scan left to right with a stack holding survivors so far, where the stack can only ever be some left-movers followed by some right-movers (anything else would already have collided). For a right-mover...