KatInterview

Runners on a Road Frozen by a Sweeping Guard

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashingsimulationhash-mapcounting

Last updated

Simulate runners advancing along a road while a guard turns at given seconds and freezes anyone in view, using parity of duplicate turn times and strict event ordering.

Runners and the Guard

A straight road covers the integer coordinates 0, 1, ..., road_length. Several runners are on it, and the guard patrols it.

Runners. Runner i starts at coordinate starts[i]. Every second, an active runner moves exactly one unit toward higher coordinates. A runner who reaches road_length has finished and is safe for the rest of the game. A runner who has been frozen (see below) never moves again.

Guard. The guard starts at guard_pos and faces guard_dir, where 1 means facing toward higher coordinates and -1 means facing toward lower ones. The list turn_times holds the seconds at which the guard turns around. The list may be unsorted, and the same second may appear more than once. Each occurrence of a second reverses the direction once, so two occurrences in the same second cancel out.

Rounds. The game lasts steps seconds, numbered t = 1, 2, ..., steps. Nothing happens at t = 0. In each second, the following happen in this exact order:

  1. Turn. Reverse the guard's direction once for every occurrence of t in turn_times.

  2. Guard moves. The guard moves one unit in its current direction. If that would leave [0, road_length], the guard stays where it is. Its facing direction is unchanged either way.

  3. Runners move. Every runner who is neither frozen nor finished moves one unit forward. A runner who reaches road_length is finished.

  4. Gaze. The guard sees a range of coordinates:

    • facing 1: every coordinate x >= guard position;
    • facing -1: every coordinate x <= guard position.

    Every runner who is not frozen, not finished (position strictly less than road_length) and inside the seen range becomes frozen at their current coordinate.

A runner who starts at road_length is already finished and is never frozen. A runner standing exactly on the guard's coordinate is seen whichever way the guard faces.

Task

Return a list with one entry per runner, in the original input order. Each entry is a pair (final_position, is_frozen), where is_frozen is True if the runner was frozen at some point and False otherwise.

Constraints

  • 1 <= road_length <= 10^5
  • 1 <= len(starts) <= 2000
  • 0 <= starts[i] <= road_length
  • 0 <= guard_pos <= road_length
  • guard_dir is either 1 or -1
  • 0 <= len(turn_times) <= 10^5
  • 1 <= turn_times[k] <= 10^5 (unsorted, duplicates allowed)
  • 1 <= steps <= 2000

Examples

Example 1

Input: simulate_runners(10, [0, 4], 5, 1, [2], 3)

Output: [[2, True], [7, False]]

Example 2

Input: simulate_runners(5, [3], 2, 1, [], 2)

Output: [[4, True]]

Hints

Hint 1

Because steps and the number of runners are both at most 2000, a direct second-by-second simulation is fast enough. The risk is in the details, not the size.

Hint 2

Do not scan turn_times every second. Count how many times each second appears once, and reverse the direction only when that count is odd.

Hint 3

Follow the four phases exactly: turn, guard move with clamping, runner move, then gaze. A runner who has just finished is never frozen, and a frozen runner never moves again.