KatInterview

Do Two Walkers Ever Share the Same Tile?

Math & Bit ManipulationModel answer
HardModel answerMath & Bit Manipulationgeometrysimulationinterval-arithmetic

Last updated

Determine if two entities moving on an infinite 2D grid ever occupy the same cell at the same integer time step.

Two Walkers on a Grid

Two robots, Alpha and Beta, are placed on an infinite 2D grid. Both begin moving at time t = 0 from their respective starting positions. Each robot follows a sequence of movement commands. A single command is a pair (direction, steps), where direction is one of 'U', 'D', 'L', 'R', and steps is a positive integer. At every integer time tick, the robot advances exactly one unit in the given direction.

Your job is to determine whether there exists any integer time t >= 0 at which both robots are on the exact same grid cell.

Function Signature

def do_walkers_meet(alpha_path, beta_path, alpha_start=(0,0), beta_start=(0,0), include_t0=True) -> bool:

Input

  • alpha_path: list of (direction, steps) tuples for Alpha.
  • beta_path: list of (direction, steps) tuples for Beta.
  • alpha_start, beta_start: starting coordinates, default (0, 0).
  • include_t0: whether t = 0 counts as a valid meeting time (default True).

Constraints

  • The total number of steps across both paths combined is at most 1,000,000.
  • Grid coordinates may be any integer (positive, negative, or zero).
  • When a robot exhausts its command list, it remains stationary at its last position.

Examples

Example 1

alpha_path  = [('R', 2)]
beta_path   = [('R', 1), ('D', 1)]
include_t0  = False

Expected output: True (Both robots are at (1, 0) at t = 1.)

Example 2

alpha_path  = [('U', 5)]
beta_path   = [('R', 5)]
include_t0  = False

Expected output: False (They start together at the origin, but t = 0 is excluded. After that they diverge.)

Edge Cases to Consider

  • Both robots start at the same position (include_t0=True → immediately True).
  • Paths of very different lengths (one robot parks early).
  • Two robots moving in parallel — they coincide for an entire window.
  • Two robots moving toward each other and crossing at a half-integer time (should return False).
  • One or both paths are empty (robots never move).

What to Produce

Write a working solution, explain the core approach and its time and space complexity, and discuss how you would handle the follow-up where each robot can move at an arbitrary integer speed per command.

Hints

Hint 1

Start with the brute-force simulation: expand every command into individual ticks and check positions. That runs in O(T) where T is total steps — up to 1,000,000 — which is acceptable but not optimal. Mention it before improving.

Hint 2

Notice that between consecutive command boundaries, both robots move at constant velocity. The relative displacement is therefore a linear function of time within each window. A meeting inside the window is just the question: does this linear function hit zero at an integer k in [lo, hi]?

Hint 3

Merge the two command lists the same way you merge two sorted arrays — always consume the shorter remaining segment. Within each merged window, solve the two-axis linear equations using only integer arithmetic (divisibility check, no floating point). Watch out for the window's inclusive lower bound when t=0 is excluded.