Solve Watcher Simulation And Integer Conversion
Hudson River TradingSoftware Engineer · Technical ScreenLast updated
This question evaluates event-driven simulation and discrete-time state modeling for tracking multiple agents under dynamic visibility constraints, together with low-level numeric string conversion and edge-case handling for 32-bit signed integers.
You are asked to solve two independent coding tasks.
Task 1: Count players that reach the finish
There is a one-dimensional track with integer positions from 0 to L. The finish line is at position L.
- There are
Nplayers.players[i]is the initial position of playeri. - Every player always wants to move to the right, one unit per second.
- A watcher stands at position
watcherPosand initially looks to the right. - At each timestamp in
turnTimes, the watcher reverses direction. The listturnTimesmay be unsorted. - During any one-second interval, a player that is currently being watched cannot move. A player is watched if:
- the watcher is looking right and the player's current position is greater than or equal to
watcherPos, or - the watcher is looking left and the player's current position is less than or equal to
watcherPos. - All players that are not watched move one unit to the right during that second.
- Multiple players may occupy the same position, and they do not block each other.
- Once a player reaches position
L, they are considered finished and remain finished.
Given L, players, watcherPos, turnTimes, and an ending time T, return how many players have reached the finish line by time T.
Define the exact event order as follows: for each integer time t from 0 to T - 1, first apply all watcher direction changes scheduled at time t, then decide which players are watched during interval [t, t + 1), then move all unwatched unfinished players one step to the right.
Task 2: Implement integer-to-string conversion
Implement a function similar to itoa:
string intToDecimalString(int x)
Given a 32-bit signed integer x, return its base-10 decimal string representation.
Requirements:
- Do not use built-in integer-to-string conversion functions.
- If
x == 0, return"0". - If
xis negative, the returned string must start with'-'. - Correctly handle the full 32-bit signed integer range, including
-2147483648.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.