Fewest Moves for a Two-Cell Rover That Can Rotate in a Grid

Last updated: November 29, 2026

Quick Overview

A rover occupies two adjacent cells of an n by n grid with obstacles, starting horizontally at (0,0)-(0,1). It can shift right or down one cell if both new cells are free, rotate clockwise from horizontal to vertical if the two cells below are free, or counterclockwise from vertical to horizontal if the two cells to the right are free. Return the fewest moves to reach (n-1,n-2)-(n-1,n-1), or -1.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A rover occupies two adjacent cells of an n by n grid with obstacles, starting horizontally at (0,0)-(0,1). It can shift right or down one cell if both new cells are free, rotate clockwise from horizontal to vertical if the two cells below are free, or counterclockwise from vertical to horizontal if the two cells to the right are free. Return the fewest moves to reach (n-1,n-2)-(n-1,n-1), or -1.

A Google onsite state space search where the challenge is encoding the rover's orientation and the exact preconditions of each move. The interviewer checks a clean state tuple, correct rotation rules, and standard BFS reasoning about optimality.

What the Interviewer Expects
  • Encode state as (row, col of the tail cell, orientation)
  • Implement shift and rotate preconditions precisely, including the 2x2 clearance for rotations
  • Run BFS since every move costs one
  • Mark visited per orientation, not just per cell
  • Return -1 when the target is unreachable
Key Topics to Cover
breadth-first search
state space search
grid simulation
motion planning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if the rover could also shift left and up?
  • How would you handle a three-cell rover?
  • How would A* with a Manhattan heuristic change the search?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Grid of 0 (free) and 1 (blocked). State (r, c, h): tail at (r, c), head at (r, c+1) if h is horizontal, else at (r+1, c). Moves: right (both cells shift right), down (both shift down), rotat...

High-Level Design

BFS from (0, 0, H). A helper free(r, c) checks bounds and obstacles. For horizontal state (r, c): right needs (r, c+2) free; down needs (r+1, c) and (r+1, c+1) free, and those same two cel...


Submit Your Answer
Markdown supported