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.
Input: (1, 0)
Expected Output: 3
Explanation: Corner case. The closest single jumps overshoot, so reaching the immediate (1,0) neighbor takes 3 moves.
Input: (2, 2)
Expected Output: 4
Explanation: Corner case. (2,2) lies just off every 2-move reach pattern and needs 4 moves.
Input: (5, 5)
Expected Output: 4
Explanation: On the main diagonal far from the origin, four balanced L-moves cover (5,5).
Input: (-3, 4)
Expected Output: 3
Explanation: Signs are irrelevant by symmetry; equivalent to (3,4) -> after swapping x>=y treat as (4,3). Three moves suffice.
Input: (0, 1)
Expected Output: 3
Explanation: Symmetric to (1,0); the same corner case, 3 moves.
Input: (2, 1)
Expected Output: 1
Explanation: (2,1) is a single legal L-move from the origin.
Input: (-2, -1)
Expected Output: 1
Explanation: Absolute values give (2,1), one move; signs do not change the count.
Input: (1000000000, 1000000000)
Expected Output: 666666668
Explanation: Large diagonal target; the O(1) formula handles 1e9 instantly where BFS would be infeasible.
Input: (1000000000, 0)
Expected Output: 500000000
Explanation: Far along one axis: roughly half as many moves as the coordinate, matching the formula.
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.