Compute Minimum L-Moves on Infinite Grid
Company: UiPath
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Compute Minimum L-Moves on Infinite Grid states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- -1e9 <= tx, ty <= 1e9
- The piece starts at the origin (0, 0).
- Eight legal moves from (x, y): (x±1, y±2) and (x±2, y±1).
- A move count always exists (the grid is infinite and the piece can reach any cell).
- A BFS solution is correct in principle but will TLE / run out of memory for |tx|, |ty| near 1e9 — an O(1) closed form is required.
Examples
Input: (0, 0)
Expected Output: 0
Explanation: Already at the origin; no moves needed.
Input: (1, 1)
Expected Output: 2
Explanation: (0,0) -> (2,-1) -> (1,1): two L-moves reach the near-diagonal cell.
Hints
- The eight moves are symmetric across both the x- and y-axes, so the answer is unchanged by the signs of tx and ty. Replace them with their absolute values immediately.
- The move set is also symmetric in swapping the two coordinates, so without loss of generality assume x >= y after taking absolute values.
- Brute-force BFS on small targets (say all x,y in 0..12) to build a ground-truth table, then look for a pattern in terms of delta = x - y and y.
- Two cells refuse to fit the bulk pattern: (1, 0) needs 3 moves and (2, 2) needs 4. Special-case them before applying the general formula.
- Branch on whether y exceeds delta = x - y: when it does you correct toward the diagonal in chunks of 3, otherwise you advance along one axis in chunks of 4 — each correction adds 2 'extra' moves.