Decide string transform with directional tokens and walls
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
You are given two equal-length strings start and target of length n, each consisting only of the characters 'L', 'R', '.', and 'X'. A dot '.' represents an empty slot; 'X' represents an immovable wall; 'L' and 'R' are tokens that may move across empty slots subject to these rules:
(
1) An 'L' token may move any number of positions left into '.', but may never move right, pass through another token, or pass through an 'X'.
(
2) An 'R' token may move any number of positions right into '.', but may never move left, pass through another token, or pass through an 'X'.
(
3) Only one token can occupy a position at any time. Determine whether target can be obtained from start by a sequence of valid moves. If yes, return true; otherwise, return false. Design an algorithm that runs in O(n) time and O(
1) extra space beyond a few pointers/counters. Explain your invariants, prove correctness informally, analyze time and space complexity, and walk through a few tricky edge cases (e.g., mismatched wall layouts, blocks separated by walls, consecutive tokens with no gaps).
Quick Answer: This question evaluates skill in string manipulation, reasoning about movement constraints, invariant formulation, and linear-time constant-space algorithm design for token movement problems.