KatInterview

Observer Simulation and Manual Integer Conversion

Math & Bit ManipulationAuto-graded · 23 test cases
MediumAuto-gradedMath & Bit Manipulationsimulationmathstring-manipulation

Last updated

Tests event-driven simulation optimization and precise numeric string formatting including signed 32-bit boundary handling.

You must solve two independent problems.

Problem 1: Players on a Track with Observer

A horizontal track has integer positions from 0 to L. The finish line is at position L.

There are N players. Each player i starts at position players[i]. Every player wants to move rightward by one position per second.

An observer stands at position watcherPos and initially looks to the right. At each timestamp in the array turnTimes, the observer reverses their viewing direction. The array turnTimes may be unsorted and may contain duplicate values.

Movement rules for each one-second interval:

During each second from time t to time t+1, a player is blocked (cannot move) if the observer is currently watching that player's position. Otherwise, the player moves one position to the right.

A player at position p is watched if and only if:

  • The observer is looking right AND p >= watcherPos, OR
  • The observer is looking left AND p <= watcherPos

Once a player reaches position L or beyond, they are finished and stop moving permanently.

Multiple players can occupy the same position.

Exact event ordering:

For each integer time t from 0 to T-1:

  1. First, apply all direction reversals scheduled at time t by processing each occurrence of t in turnTimes sequentially (each reversal toggles the direction)
  2. Then, determine which players are currently watched based on the observer's resulting direction
  3. Finally, move each non-watched, unfinished player one position to the right

Given L, players, watcherPos, turnTimes, and ending time T, return how many players have reached position L by the end of time T.

Problem 2: Convert Integer to Decimal String

Implement a function that converts a 32-bit signed integer to its decimal string representation without using built-in conversion functions.

Write int_to_string(x) where x is in the range [-2147483648, 2147483647].

Requirements:

  • Return "0" when x is zero
  • Negative numbers must start with '-'
  • Do not use Python's str(), format(), f-strings, repr(), or similar built-in conversions
  • Correctly handle -2147483648 (the minimum 32-bit signed integer)

Constraints:

  • 0 <= L <= 10^9
  • 0 <= len(players) <= 10^5
  • 0 <= players[i] <= 10^9
  • -10^9 <= watcherPos <= 10^9
  • 0 <= len(turnTimes) <= 10^5
  • 0 <= turnTimes[i] <= 10^9
  • 0 <= T <= 10^9
  • -2^31 <= x <= 2^31 - 1 (for problem 2)

Examples

Example 1

Input: count_players_finished(5, [0, 1, 5], 2, [], 5)

Output: 1

Example 2

Input: count_players_finished(5, [0, 3], 2, [0], 5)

Output: 1

Example 3

Input: count_players_finished(5, [0, 3], 2, [3, 1, 1], 5)

Output: 1

Example 4

Input: count_players_finished(10, [2, 4, 8], 5, [1, 4], 6)

Output: 1

Hints

Hint 1

For the track problem: players on different sides of the observer are blocked during different time periods. Can you count these periods instead of simulating each second?

Hint 2

A player at exactly watcherPos is always watched no matter which direction the observer faces.

Hint 3

For integer conversion: extract digits from right to left using modulo and division, then reverse. In languages with fixed-width integers, negating -2147483648 would overflow.