Karat logoKarat
Coding·40 minFree preview

Obstacle Course Run Collection: Personal Best, Best of Bests & Simulation

Three-part extension of a TypeScript Course / Run / RunCollection codebase in about 40 minutes of coding: (1) fix personalBest(), which returns 13 instead of 17 because it does not skip incomplete runs; (2) implement bestOfBests() = sum of per-obstacle minimum times across all runs, incomplete ones included (15 on the sample); (3) implement chanceOfPersonalBest(run) via a 10,000-trial Monte Carlo simulation that fills missing obstacles with uniformly sampled historical times and counts ties as success, within ±0.02.

SWE
debugging
code-reading
oop
simulation
probability
sampling
typescript
medium
Frequency
Single report
Last asked
2026-04-15
Stage
phone-screen

Problem

A reading-heavy, three-part extension task on a small existing codebase (starter code in TypeScript). The domain: software that records how long racers take to clear each obstacle of an obstacle course.

  • A run is one attempt at the whole course; a run is complete once it has a time for every obstacle, otherwise it is in progress or aborted.
  • A run collection is all runs (complete and incomplete) for one course.

Provided classes (condensed):

class Course { title: string; obstacleCount: number; equals(other: Course): boolean }

class Run {
  complete: boolean;          // true once obstacleTimes.length === course.obstacleCount
  obstacleTimes: number[];
  addObstacleTime(t: number): void;   // throws "Cannot add obstacle to complete run"
  getRunTime(): number;               // sum of times so far (partial if incomplete)
}

class RunCollection {
  runs: Run[];
  getNumRuns(): number;
  addRun(run: Run): void;             // throws if run.course differs
  personalBest(): number;             // Part 1: has a bug
  bestOfBests(): number;              // Part 2: implement
  chanceOfPersonalBest(run: Run): number;  // Part 3: implement
}

Part 1 — Fix the bug in personalBest()

personalBest() should return the best (lowest) total time among runs. On the sample data it returns 13 instead of the expected 17: incomplete runs are not excluded.

Part 2 — Implement bestOfBests()

For each obstacle, take the best (lowest) time recorded across all runs, then sum those per-obstacle bests.

Part 3 — Implement chanceOfPersonalBest(run)

Given an in-progress run, estimate the probability that it ends up beating or tying the current personal best. For every obstacle the run has not completed yet, its time is drawn uniformly from the times recorded for that obstacle in the other runs (incomplete runs included). Use simulation:

  • Over 10,000 trials: for each incomplete segment, pick a time at random from previous runs; check whether the completed total is better than or equal to the current personal best.
  • Return the fraction of trials that hit a personal best. The answer must consistently land within ±0.02 of the true probability.

Examples

 Obstacles:    O1  O2  O3  O4
    Run 1:      3   4   5   6    (total: 18 seconds)
    Run 2:      4   4   4   5    (total: 17 seconds)
    Run 3:      4   5   4   6    (total: 19 seconds)
    Run 4:      5   5   3        (13 seconds, but run is incomplete)
  • getNumRuns() == 4, personalBest() == 17, bestOfBests() == 15.
  • Part 3, test 1: runs [3,3,2], [3,3,3]; in-progress run [3,3] → last obstacle drawn from {2, 3} → expected chance ≈ 0.50.
  • Part 3, test 2: runs [3,3,2,3], [3,3,3,2], [5,5,2]; in-progress run [3,3] → obstacle 3 drawn from {2, 3, 2}, obstacle 4 from {3, 2}; chance of finishing in ≤ 11 is 5/6 ≈ 0.8333 (accepted range 0.81333–0.85333).
  • Part 3, test 3: 10 obstacles, 10 runs of mixed lengths, in-progress run [19, 19, 19] → expected ≈ 0.95.

Pitfalls

  • Only about 40 minutes of coding time, much of it spent reading the spec and existing classes; it is easy to finish Parts 1–2 and run out of time on Part 3.
  • Part 1: the fix is to filter to complete runs before taking the minimum; the incomplete run's partial total (13) is the trap.
  • Part 2 includes incomplete runs — a partial run still contributes its recorded obstacle times (O3's best of 3 comes from the incomplete Run 4). Index the per-obstacle array by position (best[i] = Math.min(best[i], times[i])) rather than comparing whole arrays.
  • Part 3: an obstacle's candidate pool is only the runs that actually recorded that obstacle — an incomplete run contributes to early obstacles but not later ones (test 2's obstacle 4 has only two candidate times).
  • Part 3 compares with "better than or equal to" the current personal best; ties count as success (test 1 depends on it).
  • The ±0.02 tolerance is checked by the test harness, so the trial count matters — use the full 10,000 trials.

Preparation

  • Re-implement all three parts against the sample data above in TypeScript within 40 minutes, including a reading pass over the class definitions.
  • Write the Part 3 simulation and verify it reproduces ≈0.50 and ≈0.833 on tests 1 and 2.
  • On a fast first read of a long spec, mark which computations include incomplete runs and which are complete-only.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...