Google Online Coding Challenge for SDE, 21 Oct

Anonymous User
475
Oct 21, 2026

Question asked in Google Online Coding Challenge.

Robot Assembly Line Conflicts

Problem Statement

You are managing a linear assembly line with n robots positioned at consecutive integer coordinates 0, 1, 2, ..., n-1. Each robot has an initial movement direction represented by a character in a string called robotMovements.

Each robot behaves as follows:

  • R: Moves right, toward higher indices.
  • L: Moves left, toward lower indices.
  • S: Remains stationary and does not move.

All moving robots travel at the same constant speed.

When two robots collide, they both stop permanently at the collision point and become stationary.

Your task is to calculate the total number of collision events that occur on the assembly line.

Collision Rules

1. Head-on collision: When a right-moving robot (R) collides with a left-moving robot (L), count 2 collision events, representing the impact experienced by both robots.

2. Moving robot hits a stationary robot: When a moving robot collides with a stationary robot (S), count 1 collision event.

3. Chain reactions: Robots that stop after a collision become stationary obstacles. They can cause additional collisions with other approaching robots. All such collision events must be counted.

Function Description

Implement the function:

calculateAssemblyLineConflicts(robotMovements)

Parameter

- robotMovements: A string of length n, where each character is L, R, or S, representing a robot's initial movement direction.

Return Value

Return an integer representing the total number of collision events.

Input Format

A single string, robotMovements, representing the initial movement directions of the robots.

Output Format

Print a single integer representing the total number of collision events.

Constraints

  • 1 ≤ n ≤ 10⁵
  • robotMovements[i] is either L, R, or S.

Sample Input

RLRSLL

Sample Output

5

Sample Explanation

The robots are positioned as follows:

R L R S L L

  • The first R and L collide head-on, contributing 2 events.
  • The next R collides with the stationary S, contributing 1 event.
  • The remaining two L robots eventually collide with robots that have stopped, contributing 1 event each.

Total collision events = 2 + 1 + 1 + 1 = 5.

Comments (2)