KatInterview

Fleet Command Console for Grid-Walking Robots

Data Structure DesignModel answer
EasyModel answerData Structure Designsimulationhash-mapcommand-parsing

Last updated

Design a text-driven controller that manages several robots on an unbounded grid, handling turns, steps, selection, error cases and cell collisions.

Fleet Command Console

Build a small text-driven console that controls robots moving on an infinite 2D grid. Build it in three stages, each extending the previous one.

Stage 1: one robot

A robot starts at (0, 0) facing North. It understands three commands:

  • L: rotate 90 degrees counter-clockwise (N -> W -> S -> E -> N)
  • R: rotate 90 degrees clockwise (N -> E -> S -> W -> N)
  • M: advance one cell in the direction it faces

After every command, print the robot's current position and heading.

Stage 2: several robots

Add commands to manage a fleet:

  • CREATE <id>: add a new robot in the default state (0, 0, N)
  • DELETE <id>: remove a robot
  • SELECT <id>: make that robot the active one
  • L, R, M act on the active robot only

Decide and document what happens on invalid operations, for example selecting or deleting an id that does not exist, creating a duplicate id, or sending L/R/M when nothing is selected.

Stage 3: no collisions

If the active robot issues M and the cell it would enter is occupied by another robot, the move fails and the robot keeps its position and heading. The console must still print the state after the command, along with a status or error message of your choosing.

What you must specify

You choose the exact I/O format (a REPL or lines read from stdin), but it has to be unambiguous and testable. State clearly:

  • the accepted command syntax,
  • what is printed after each command (position, heading, and optionally a status),
  • your policy for each invalid operation.

Rules

  • The grid is unbounded, so coordinates can be negative.
  • Robot ids are unique strings.
  • Keep the design clean and unit-testable, and include tests.

Sample session

CREATE a
SELECT a
M
R
M
CREATE b
SELECT b
M
SELECT a
M      # fails if b occupies the target cell

Your printed format may differ from any particular one, but it must show position and heading after every command.

Expected: a short design explanation, the implementation, its complexity, and a few tests.

Hints

Hint 1

Represent a robot as (x, y, heading). Keep headings in the order N, E, S, W so that R is +1 and L is -1 modulo 4, and store a direction-to-offset table for moving.

Hint 2

Put all logic in one method that takes a command string and returns the output string. The controller holds a dict from id to robot plus the currently selected id. This lets you unit test without stdin.

Hint 3

For collision checks, keep a second dict from (x, y) to robot id that always mirrors robot positions. Compute the target cell, check it before changing anything, and update the index on create, delete and each successful move. Decide what happens when CREATE is issued while (0, 0) is occupied.