← Questions

Find a target in an unknown grid with a robot API

0
MetaCodingLast asked Nov 2025

A coding interview question asked in Meta's technical phone screen, last asked Nov 2025. It tests dfs, backtracking and grids.

A mouse sits somewhere in a maze you cannot see, and a piece of cheese is somewhere in it. You don't know the maze's size, its walls, or where the mouse starts. You can only control the mouse through its API. Write the search that stops as soon as the mouse is on the cheese.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • mouse.move() -> bool — steps one cell in the direction the mouse faces. Returns False and stays put if a wall or the edge blocks it.
  • mouse.turn_left() -> None and mouse.turn_right() -> None — turn 90 degrees in place.
  • mouse.has_cheese() -> bool — whether the current cell holds the cheese.
  • find_cheese(mouse) -> bool — moves the mouse until it stands on the cheese and returns True, or returns False once every reachable cell has been visited.

Example

# . open, # wall, M mouse (facing up), C cheese
# # # # #
# M . # #
# . # C #
# . . . #
# # # # #

find_cheese(mouse)  ->  True     # e.g. down, down, right, right, up

Your code never sees this map. It only learns about walls when move() returns False.

Constraints

  • The maze is finite. Cells are connected through their four sides.
  • Each API call is a physical step, so needless moves cost time.

Clarifying questions

  • Does the mouse know which way it faces at the start? (It doesn't need to: track direction relative to the start.)
  • Is the cheese always reachable?
  • Is the cost the number of move() calls, or all API calls?
  • Should the mouse stop on the cheese, or come back to the start?

What a strong answer covers

  • Coordinates relative to the start, (0, 0) facing "up", and a visited set of those coordinates.
  • Depth-first search over the four directions, with physical backtracking: turn around, move back, and turn to restore the heading after each branch.
  • Keeping the tracked heading in sync with every turn, which is where most bugs hide.
  • O(V) moves and O(V) memory for V open cells reachable from the start. Each cell is entered once and backed out of once.
  • Unit tests with a fake mouse over a known grid: cheese at the start, unreachable cheese, a long corridor, a maze with loops.

Follow-ups

  • Return the path from the start to the cheese.
  • After finding it, bring the mouse back to the start by the shortest known path.
  • Write an iterative version for very large mazes, where recursion would overflow.
  • Which unit tests would you write for the navigation code?

Where it's asked

  • Meta · Technical phone screen · Last asked Nov 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted

What it tests

dfs · backtracking · grids · interactive

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding