← Questions

Two cars to their destinations without collisions

0
GoogleCoding

A coding interview question asked at Google. It tests bfs, grid and state space search.

A parking robot controls two self-driving cars on a rectangular map. The map is a list of equal-length strings. Each character is one cell:

  • . is road.
  • # is a wall. Cars cannot enter it.
  • a and b are the starting cells of car a and car b.
  • A and B are the destinations of car a and car b.

Each symbol a, b, A and B appears exactly once. Cells marked with these letters are road, so either car may drive over them.

Time moves in steps. On every step, both cars act at the same time. Each car either stays where it is or moves one cell up, down, left or right, inside the map and not into a wall. A step is not allowed if:

  • both cars end the step in the same cell, or
  • the cars swap cells, because they would pass through each other.

A car may move into the cell that the other car leaves during the same step.

Implement min_steps(grid: list[str]) -> int. It returns the fewest steps after which car a is on A and car b is on B at the same time. Return -1 if this is impossible.

Constraints: 1 <= rows, cols <= 50.

Example 1

grid = ["abAB"]

Output: 2. Both cars drive right together. On each step, a enters the cell that b has just left.

Example 2

grid = ["BabA",
        ".#.."]

Output: 4. In the top row alone the cars would have to pass each other, which is not allowed. Car b steps down into the lower row while a drives right. Then a reaches A, and b drives back up and left to B. With only the row "BabA", the answer is -1.

Clarifying questions

  • Is the answer counted in steps where both cars act, even if one car waits?
  • Once a car reaches its destination, may it leave again (it may need to move out of the way)?
  • Is staying in place always allowed, even for both cars at once?
  • Can a destination cell be the other car's start cell?

What a strong answer covers

  • Searches the joint state (position of a, position of b) with BFS: at most (50 * 50)^2, about 6 million states, with 25 move pairs each.
  • Generates both cars' moves together and rejects steps where they end in the same cell or swap cells, while allowing one to enter the cell the other leaves.
  • Encodes states as integers and uses a flat visited array to fit the scale in memory and time.
  • Stops at the first state with a on A and b on B, and returns -1 when the BFS runs out (the single-row case in Example 2).
  • Tests a corridor where the cars must pass each other, the follow-the-leader case in Example 1, and a car that must leave its goal to let the other through.

Follow-ups

  • Return the actual sequence of moves.
  • Speed it up with bidirectional BFS or A* with a sum-of-distances heuristic.
  • Generalise to k cars, and discuss why the joint state space explodes (prioritised planning, conflict-based search).

Where it's asked

What it tests

bfs · grid · state space search

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