The Count Collisions on a Road interview question simulates cars moving on a single-lane road. Each car is moving either Left (L), Right (R), or is Stationary (S). When two moving cars collide, or a moving car hits a stationary one, they become stationary. You need to count the total number of collisions. A collision between two moving cars (RL) counts as 2, and a moving car hitting a stationary car (RS or SL) counts as 1.
Amazon and Bloomberg ask the Count Collisions on a Road coding problem to test your ability to simplify a complex simulation. While it looks like you need to track every car's movement over time, the problem can be reduced to a single-pass string processing task. It evaluates your "systems thinking" and ability to identify which cars will eventually collide.
This is a Simulation / String Manipulation problem.
LL...) and cars on the far right moving Right (...RR) will never collide.L or R) in the middle will eventually collide with something (either a stationary car or another car moving towards it).L or R in the trimmed string contributes exactly 1 collision point.road = "RLRSLL"
R and ends with L).L and R in the remaining string:
R, L, R, L, L (5 moving cars).S in the middle doesn't count as a collision point but causes others to collide.RL at the start is 2 points, but our logic handles it because R and L both count as 1.RL collision (it's 2 points) or forgetting that once a car collides, it stays stationary.Always look for the "equilibrium" state in simulation problems. Ask yourself: "Which elements are guaranteed to change, and what will they look like when they stop?"
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Remove K-Balanced Substrings | Medium | Solve | |
| Remove All Occurrences of a Substring | Medium | Solve | |
| Removing Stars From a String | Medium | Solve | |
| Resulting String After Adjacent Removals | Medium | Solve | |
| Clear Digits | Easy | Solve |