FastPrepFix the First Robot Instruction
Problems/Fix the First Robot Instruction

Fix the First Robot Instruction

Shopify logoShopify● MediumFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The movement rules and worked example match the screenshot at about 98%. Bounds and uniqueness of the replacement are explicit practice assumptions.

A robot starts at (0,0), facing right. Moving right increases x; moving up increases y. It follows these commands:

  • FORWARD: move one step in the direction it faces.
  • BACK: move one step backward without changing the direction it faces.
  • TURN LEFT: turn left by 90 degrees without moving.
  • TURN RIGHT: turn right by 90 degrees without moving.

The supplied sequence ends at the wrong position. Replace only its first command so that the final position equals target. Passing through the target before the sequence ends does not count.

Return the exact message Replace instruction 1 with COMMAND, replacing COMMAND with the selected command. The index is one-based. For this exercise, assume exactly one command different from the original first command works.

Function

fixFirstInstruction(instructions: String[], target: int[]) → String

Examples

Example 1

instructions = ["FORWARD","TURN RIGHT","FORWARD","TURN RIGHT","FORWARD","FORWARD","FORWARD","FORWARD"]target = [-1,4]return = "Replace instruction 1 with TURN RIGHT"

The original sequence ends at (-3,-1). Replacing its first command with TURN RIGHT makes the robot face down, then the suffix moves it one step left and four steps up. It ends at (-1,4).

Example 2

instructions = ["BACK","FORWARD"]target = [2,0]return = "Replace instruction 1 with FORWARD"

Replace BACK with FORWARD. The two forward steps move right from (0,0) to (2,0).

Constraints

  • 1 <= instructions.length <= 10000
  • target.length == 2
  • -10000 <= target[i] <= 10000
  • Each instruction is FORWARD, BACK, TURN LEFT, or TURN RIGHT.
  • The original sequence misses target, and exactly one different first instruction makes its final position equal target.

More Shopify problems

  • Deterministic Secret Santa AssignmentPHONE SCREEN · Seen Oct 2026
  • Mars Rover Robot ControllerPHONE SCREEN · Seen Oct 2026
  • Choose Items for a BackpackOA · Seen May 2026
All Shopify interview questionsSee Shopify hiring insights
Saved
public String fixFirstInstruction(String[] instructions, int[] target) {
    // Write your code here.
}
Checking account…