Implement chance of a personal best
Problem Description
You are tasked with implementing a method to calculate the probability of an in-progress run achieving a personal best on an obstacle course. The system models obstacle courses and individual runs through them.
- A course has a fixed number of obstacles.
- A run records the time spent on each obstacle in order. A run can be complete or incomplete.
- A run collection stores multiple runs for the same course.
For any given obstacle index i, the historical times available for that obstacle are derived from all other runs in the collection that have recorded a time for obstacle i, including incomplete runs.
Required Behavior
Implement the method chance_of_personal_best(run) within the RunCollection class. Given an in-progress run, the method must:
- Perform 10,000 simulation trials.
- In each trial, for every obstacle not yet completed in the current in-progress run, randomly select one historical time for that specific obstacle from the other runs in the collection that have a recorded time for it.
- Combine the current run's already recorded obstacle times with the newly sampled times for the remaining obstacles to form a simulated total run time.
- Check whether this simulated total time is less than or equal to the current personal best.
- Return the fraction of trials that resulted in a personal best (or tied it) as a floating-point probability.
Important Details
- The current personal best is defined as the minimum total time among completed runs only within the
RunCollection. - When sampling future obstacle times, include times from incomplete historical runs if they reached that particular obstacle.
- Do not sample from the in-progress run that is currently being evaluated.
- The returned probability should typically be within 0.02 of the true probability, given the 10,000 trials.
Example
Consider a course with 3 obstacles and the following historical runs:
[3, 3, 2](completed)[3, 3, 3](completed)
The current in-progress run is [3, 3] (meaning the first two obstacles are done, the third is pending).
The personal best among completed runs is 8 (from [3,3,2]). For the last obstacle (index 2), the historical times available from other runs are {2, 3}. Therefore, a simulated completed run will either be [3,3,2] or [3,3,3], each with equal probability. The chance of matching or beating the personal best (8) is approximately 0.5 (only [3,3,2] beats or ties).
Starter Structure
Assume the following classes already exist:
Course(title, obstacle_count)Run(course)with fields and methods:coursecomplete(boolean)obstacle_times(list of integers)add_obstacle_time(t)get_run_time()(sumsobstacle_times)
RunCollection(course)with field:runs(list ofRunobjects)
Your task is to write the chance_of_personal_best method for RunCollection.
AI interview prep
Core dilemma
Balancing the computational cost of performing numerous simulations with efficient data retrieval and statistical accuracy for dynamic sampling, especially as the number of runs and obstacles scales.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement chance of a personal best
Problem Description
You are tasked with implementing a method to calculate the probability of an in-progress run achieving a personal best on an obstacle course. The system models obstacle courses and individual runs through them.
- A course has a fixed number of obstacles.
- A run records the time spent on each obstacle in order. A run can be complete or incomplete.
- A run collection stores multiple runs for the same course.
For any given obstacle index i, the historical times available for that obstacle are derived from all other runs in the collection that have recorded a time for obstacle i, including incomplete runs.
Required Behavior
Implement the method chance_of_personal_best(run) within the RunCollection class. Given an in-progress run, the method must:
- Perform 10,000 simulation trials.
- In each trial, for every obstacle not yet completed in the current in-progress run, randomly select one historical time for that specific obstacle from the other runs in the collection that have a recorded time for it.
- Combine the current run's already recorded obstacle times with the newly sampled times for the remaining obstacles to form a simulated total run time.
- Check whether this simulated total time is less than or equal to the current personal best.
- Return the fraction of trials that resulted in a personal best (or tied it) as a floating-point probability.
Important Details
- The current personal best is defined as the minimum total time among completed runs only within the
RunCollection. - When sampling future obstacle times, include times from incomplete historical runs if they reached that particular obstacle.
- Do not sample from the in-progress run that is currently being evaluated.
- The returned probability should typically be within 0.02 of the true probability, given the 10,000 trials.
Example
Consider a course with 3 obstacles and the following historical runs:
[3, 3, 2](completed)[3, 3, 3](completed)
The current in-progress run is [3, 3] (meaning the first two obstacles are done, the third is pending).
The personal best among completed runs is 8 (from [3,3,2]). For the last obstacle (index 2), the historical times available from other runs are {2, 3}. Therefore, a simulated completed run will either be [3,3,2] or [3,3,3], each with equal probability. The chance of matching or beating the personal best (8) is approximately 0.5 (only [3,3,2] beats or ties).
Starter Structure
Assume the following classes already exist:
Course(title, obstacle_count)Run(course)with fields and methods:coursecomplete(boolean)obstacle_times(list of integers)add_obstacle_time(t)get_run_time()(sumsobstacle_times)
RunCollection(course)with field:runs(list ofRunobjects)
Your task is to write the chance_of_personal_best method for RunCollection.