FastPrepRepair One Movement Instruction
Problems/Repair One Movement Instruction

Repair One Movement Instruction

OpenAI logoOpenAI● MediumFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source establishes replacing one instruction, replaying the route, avoiding obstacles, and reaching the target. The judged grid symbols, compact command alphabet, initial upward orientation, and invalid-step rule complete the task; the core behavior is an approximately 90% match.

You are given a rectangular character grid and a movement program.

  • S is the unique start cell.
  • T is the unique target cell.
  • # is an obstacle.
  • . is an open cell.

The robot starts on S facing up. Each character of instructions is one command:

  • F: move one cell forward.
  • B: move one cell backward without changing direction.
  • L: turn left in place.
  • R: turn right in place.

You must replace exactly one instruction with a different command from F, B, L, and R. A candidate replacement is invalid if any move leaves the grid or enters an obstacle.

Return true if some valid replacement makes the robot finish on T after executing every instruction. Its final facing direction does not matter.

Function

canReachTargetWithOneReplacement(grid: String[], instructions: String) → boolean

Examples

Example 1

grid = [".T.","...",".S."]instructions = "FR"return = true

Replace R with F. The robot moves up twice and finishes on T.

Example 2

grid = ["T#","S."]instructions = "F"return = false

The original instruction reaches T, but it must be replaced. Each different command either turns in place or leaves the grid.

Example 3

grid = ["..T",".#.","S.."]instructions = "FLRFF"return = true

Replace the second command L with F. The repaired program FFRFF travels around the obstacle.

Constraints

  • 1 ≤ grid.length, grid[r].length ≤ 100
  • Every row has the same length.
  • The grid contains exactly one S and one T.
  • Every other cell is . or #.
  • 1 ≤ instructions.length ≤ 2000
  • instructions contains only F, B, L, and R.

More OpenAI problems

  • Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public boolean canReachTargetWithOneReplacement(String[] grid, String instructions) {
        // write your code here
    }
}
Checking account…