KatInterview

Track and Analyze Obstacle Course Runs

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designdesign-dsarraysstatistics

Last updated

Simulate obstacle course data structures, track per-stage times, calculate target metrics, and run Monte Carlo probability estimation.

Design an analytics engine to evaluate athlete performance on a multi-stage obstacle course.

The system processes commands operating on two core domain concepts:

  1. Run: Represents a single athlete's attempt on a course with course_obstacle_count total obstacles.

    • It maintains an ordered sequence of completed obstacle durations (in seconds).
    • A run is considered complete if the number of recorded obstacle durations equals course_obstacle_count.
    • Adding an obstacle time to an already complete run raises a ValueError.
    • Summing recorded times gives the current elapsed duration of the run.
  2. RunCollection: Manages a set of runs sharing the same course_obstacle_count.

    • personal_best(): Returns the minimum total completion time among complete runs only. If no complete runs exist, returns None.
    • best_of_bests(): Calculates the ideal hypothetical completion time by taking the minimum observed time for each obstacle index i (from $0$ to $\text{course_obstacle_count} - 1$) across all registered runs (both complete and incomplete) that reached index i. If any obstacle index has zero recorded times across all runs, returns None.
    • chance_of_personal_best(test_run): Uses Monte Carlo simulation ($10{,}000$ iterations) to estimate the probability that an in-progress run will equal or beat (i.e., total time $\le$) the current personal_best().
      • For each uncompleted obstacle index $i$ in test_run, sample a duration uniformly at random (with replacement) from the set of all historical times recorded at index $i$ across all runs in the collection.
      • The simulation uses pseudo-random sampling with a fixed random seed (seed=0).
      • If test_run is already complete, raise a ValueError.
      • If personal_best() is None (no finished runs exist), return 0.0.
      • If any remaining obstacle index has no historical recorded times across registered runs, raise a ValueError.
      • Returns the fraction of trials where the simulated total run time is $\le$ personal_best().

You are to implement run_collection_results(course_obstacle_count, operations, arguments) which simulates a sequence of operational instructions and returns a list containing the result of each step (or None for operations that produce no direct output).

Supported Operations

  • "create_run": args = [run_id] — Instantiates a new run labeled run_id.
  • "add_obstacle_time": args = [run_id, time] — Appends time to run run_id.
  • "add_run": args = [run_id] — Registers run run_id into the collection.
  • "get_run_time": args = [run_id] — Returns the sum of recorded times for run_id.
  • "is_complete": args = [run_id] — Returns True if run_id has completed all obstacles, False otherwise.
  • "personal_best": args = [] — Evaluates and returns the best time among complete runs in the collection.
  • "best_of_bests": args = [] — Evaluates and returns the theoretical best time across all obstacle stages.
  • "chance_of_personal_best": args = [run_id] — Estimates the probability of run_id matching or breaking personal_best().

Constraints

  • $1 \le \text{course_obstacle_count} \le 100$
  • $1 \le \text{len(operations)} \le 1000$
  • All obstacle time inputs are positive integers.
  • Operations reference valid run_id values created earlier.

Examples

Example 1

Input: run_collection_results(3, ['create_run', 'add_obstacle_time', 'add_obstacle_time', 'create_run', 'add_obstacle_time', 'add_obstacle_time', 'add_obstacle_time', 'add_run', 'create_run', 'add_obstacle_time', 'add_obstacle_time', 'add_run', 'add_run', 'personal_best', 'best_of_bests'], [['a'], ['a', 10], ['a', 5], ['b'], ['b', 4], ['b', 8], ['b', 6], ['b'], ['c'], ['c', 7], ['c', 3], ['c'], ['a'], [], []])

Output: [None, None, None, None, None, None, None, None, None, None, None, None, None, 18, 13]

Example 2

Input: run_collection_results(3, ['create_run', 'add_obstacle_time', 'create_run', 'add_obstacle_time', 'add_run', 'create_run', 'add_obstacle_time', 'add_obstacle_time', 'add_run', 'create_run', 'add_obstacle_time', 'add_obstacle_time', 'add_run', 'best_of_bests'], [['a'], ['a', 9], ['b'], ['b', 2], ['b'], ['c'], ['c', 8], ['c', 4], ['c'], ['d'], ['d', 6], ['d', 5], ['d'], []])

Output: [None, None, None, None, None, None, None, None, None, None, None, None, None, None]

Hints

Hint 1

Make sure personal_best only considers finished runs—partial runs with fewer completed obstacles have smaller sums that must be ignored.

Hint 2

For best_of_bests, gather all recorded times at each position index i across both complete and incomplete runs. If any position has no recorded times, return None.

Hint 3

Pre-compute the candidate pool of historical times for each missing stage index before running the 10,000 Monte Carlo trials to maintain good performance.