Karat / VO 面试真题解析:Obstacle Course Run Collection 数据建模与统计

13次阅读
没有评论

We are writing software to collect and manage data on how fast racers can complete obstacle courses.

An obstacle course is a series of difficult physical challenges (like walls, hurdles, and ponds) that a racer must go through. Each course consists of multiple obstacles.

The software stores how long it takes for racers to finish each obstacle, and provides useful statistics based on those times.

Definitions:

  • A run is a particular attempt to complete an entire obstacle course.
  • A run collection is a group of runs on a particular course by the user.
  • An obstacle is a portion of a course.

We track how long it takes to finish each portion of the course.

For example, here are some times for an obstacle course with four obstacles:

Obstacles:  01  02  03  04
Run 1:       3   4   5   6   (total: 18 seconds)
Run 2:       4   4   4   5   (total: 17 seconds)
Run 3:       5   5   3       (13 seconds, but run is incomplete)

All of these runs for one obstacle course (including the incomplete run) make up a run collection.

You are given the following classes:

  • Course: data about a particular course.
  • Run: data and methods about a single run of a course.
  • RunCollection: data for a number of runs of a particular course, and methods for getting useful statistics about all runs of a course.

Run should support adding obstacle times one by one, track whether the run is complete, and return the total elapsed time so far.

RunCollection should validate that all added runs belong to the same course and be able to compute the personal best time across all runs.

Example test data:

  • obstacle_data = [[3, 4, 5, 6], [4, 4, 4, 5], [5, 5, 3]]
  • test_course = Course("Test course", 4)
  • Expected number of runs: 3
  • Expected personal best: 17

这道题本质上是在设计一组面向对象的数据模型来记录障碍赛跑成绩:`Course` 表示赛道,`Run` 表示一次完整或未完成的尝试,`RunCollection` 表示同一赛道上的多次尝试集合。核心实现通常围绕“逐个添加障碍时间、判断是否完成、累计总用时、校验是否属于同一赛道、统计最优成绩”展开。`Run` 需要在达到赛道障碍数后标记为完成,并在已完成后禁止继续添加;`RunCollection` 则要用列表保存所有 `Run`,并通过遍历计算 `personal_best()`,样例中 `[[3,4,5,6],[4,4,4,5],[5,5,3]]` 的最佳成绩为 17。

正文完
 0