Hudson River Trading logoHudson River Trading
CodingFree preview

Watcher Simulation

Simulate players moving toward an endpoint while a moving watcher changes direction and freezes players in its line of sight.

SWE
MLE
simulation
Frequency
Medium
Last asked
2026-07-27
Stage
phone-screen

Requirements

  • Input: endpoint L, player starting positions, watcher starting position W, direction-change timestamps, and ending time T.
  • Players move right at one unit per time unit when not watched. A watched player remains still. The watcher moves at the same speed in its facing direction.
  • Flip the watcher’s direction at the supplied timestamps; the timestamp list may be unsorted.
  • Return the number of players reaching L by T.
  • A player at the watcher’s exact position can move.
  • Follow-up: describe how to handle continuous time rather than discrete time steps.

Examples

T = 2, players = [1, 4], W = 3, L = 5, and direction-change times [1] give output 1.

Notes

The common form starts with the watcher facing left.

  • Minority variant: initial direction is supplied as left or right; clarify before coding. A variant takes a list of watcher positions; the single-watcher form is described above. Clarify the number of watchers and event ordering at each timestamp. The prompt is often spoken aloud; candidates have needed to take their own notes.

For a discrete-time baseline, sort direction-change times and explicitly choose when a flip takes effect, when visibility is evaluated, and whether movement is simultaneous. Use one old-state snapshot per step so player iteration order cannot change the result. With one watcher, a player strictly on the facing side is frozen; equality permits movement. Record arrival once and keep finished players out of further updates. For P players, E direction changes, and T unit steps, sorting plus simulation takes O(E log E + PT + E) time and O(P + E) storage.

For continuous time, work through intervals of constant direction and split a player's trajectory at watcher contact or endpoint arrival. Advancing directly to direction changes without checking these contacts can miss a change in visibility. The exact event-ordering and multi-watcher rules still need clarification before treating either implementation as definitive.

Preparation

  • In 20 minutes, implement the single-watcher discrete baseline under a written timing convention; trace the supplied example and separately test equality, immediate arrival, and unsorted flips.
  • In 15 minutes, draw relative-position trajectories for both directions, then identify every contact and arrival event before attempting the continuous-time follow-up.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...