Robot Stops Left or Right (Forward Deployed Engineer)
Updated
Problem statement
Given a string commands of L and R moves, return "L" if a robot stops to the left of its starting position, "R" if it stops to the right, or an empty string "" if it stops at its starting position.
The robot moves along a horizontal line and carries out the commands in order. Each L moves it one step to the left, and each R moves it one step to the right. The robot stops after the last command.
Note: a solution with time complexity no worse than O(commands.length^2) is fast enough.
Function
solution(commands: String) → StringExamples
Example 1
commands = "RLLRLL"return = "L"The first two commands, R and then L, bring the robot back to its starting position. The third and fourth commands, L and then R, bring it back again. The last two commands are both L, so the robot stops two steps to the left.
Example 2
commands = "LRRRL"return = "R"There are three R moves and two L moves. The robot stops one step to the right of its starting position.
Example 3
commands = "LLRR"return = ""Two steps to the left and then two steps to the right bring the robot back to its starting position, so the answer is an empty string.
Constraints
1 <= commands.length <= 1000commandscontains only the charactersLandR.
More Boston Consulting Group interview questions
- Build Taxi Driver Performance FeaturesOA · Seen Nov 2026
- Summarize Taxi Drivers and RidesOA · Seen Nov 2026
- Competition Winner and Runner-Up (Forward Deployed Engineer)OA · Seen Nov 2026
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)OA · Seen Nov 2026
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)OA · Seen Nov 2026
- Predict Taxi Driver ClassesOA · Seen Oct 2026
- Prepare Taxi Driver Classification DataOA · Seen Oct 2026
- Even-Position MonotonicityOA · Seen Jul 2026
- Shortest Distances From an Adjacency MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Branch with the Most FilesCapital One · OACapital One interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- REST API: Highest International StudentsRippling · OARippling interview questions
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Filter Isolated Interior Signal BitsSEDEMAC · OA
- Scale Ingredient QuantitiesUpstart · OAUpstart interview questions
- Telephone Keypad String EncodingEpic · OAEpic interview questions
- Count Invalid Log GroupsIBM · OAIBM interview questions
- Count Case-Insensitive TripletsTikTok · OATikTok interview questions
- Simulate a Deterministic Finite AutomatonPoint72 · OAPoint72 interview questions