Assort Health:预约时间段清洗与聚合 – 代面试 – 面试辅助 – 面经 – 一亩三分地

英文原题

Part 1 – Parse and Validate:
Remove slots with missing fields, invalid ISO timestamps, or durations other than exactly 10 minutes.

Part 2 – Aggregate:
Group consecutive valid 10-minute slots from the same provider and appointment type into bookable 30-minute blocks.

Overall:
Implement get_available_slots(raw_slots) to return clean, valid, and bookable appointment availability without assuming the input is sorted.


题目介绍

实现:

get_available_slots(raw_slots)

输入是一组预约时间段,每条数据包含:

{
    "provider_id": "doctor_1",
    "appointment_type": "consultation",
    "start": "2026-07-20T09:00:00Z",
    "end": "2026-07-20T09:10:00Z"
}

函数需要完成两个步骤:

  1. 清洗无效数据。
  2. 将连续的 10 分钟时间段聚合成可预约的 30 分钟时间段。

Part 1:清洗数据

以下数据需要过滤:

  • 缺少必要字段
  • startend 不是合法 ISO 时间
  • 时间段长度不是严格的 10 分钟

例如:

{
    "provider_id": "doctor_1",
    "appointment_type": "consultation",
    "start": "invalid",
    "end": "2026-07-20T09:10:00Z"
}

开始时间无法解析,应直接过滤。

下面这条数据同样需要过滤:

{
    "provider_id": "doctor_1",
    "appointment_type": "consultation",
    "start": "2026-07-20T09:00:00Z",
    "end": "2026-07-20T09:15:00Z"
}

持续时间为 15 分钟,不符合要求。


Part 2:聚合预约时间

三个连续的 10 分钟时间段可以组成一个 30 分钟预约。

例如:

09:00–09:10
09:10–09:20
09:20–09:30

聚合后得到:

09:00–09:30

三个时间段需要满足:

  • provider 相同
  • appointment type 相同
  • 前一个时间段结束时间等于后一个时间段开始时间

例如:

09:00–09:10
09:20–09:30
09:30–09:40

由于缺少:

09:10–09:20

因此不能组成 30 分钟预约。


输入无序

题目明确说明:

without assuming the input is sorted

输入可能是:

09:20–09:30
09:00–09:10
09:10–09:20

聚合之前需要先排序。

排序字段可以是:

provider_id
appointment_type
start

同一位医生、同一种预约类型分别排序。


示例

输入:

raw_slots = [
    {
        "provider_id": "p1",
        "appointment_type": "general",
        "start": "2026-07-20T09:20:00Z",
        "end": "2026-07-20T09:30:00Z",
    },
    {
        "provider_id": "p1",
        "appointment_type": "general",
        "start": "2026-07-20T09:00:00Z",
        "end": "2026-07-20T09:10:00Z",
    },
    {
        "provider_id": "p1",
        "appointment_type": "general",
        "start": "2026-07-20T09:10:00Z",
        "end": "2026-07-20T09:20:00Z",
    },
]

输出:

[
    {
        "provider_id": "p1",
        "appointment_type": "general",
        "start": "2026-07-20T09:00:00Z",
        "end": "2026-07-20T09:30:00Z",
    }
]

解题思路

1. 校验字段

确认每条记录包含:

provider_id
appointment_type
start
end

字段缺失直接过滤。

2. 解析时间

将字符串解析为时间对象。

解析失败直接过滤。

3. 校验持续时间

计算:

duration = end_time - start_time

判断:

duration == timedelta(minutes=10)

只有严格等于 10 分钟的数据保留。

4. 分组

按照:

(provider_id, appointment_type)

进行分组。

不同医生或不同预约类型分别处理。

5. 排序

每组按照开始时间排序。

例如:

09:20
09:00
09:10

排序后:

09:00
09:10
09:20

6. 聚合

遍历排序后的时间段。

当满足:

current.end == next.start

说明两个时间段连续。

累计三个连续时间段即可生成一个 30 分钟预约。


边界情况

连续六个时间段:

09:00–09:10
09:10–09:20
09:20–09:30
09:30–09:40
09:40–09:50
09:50–10:00

可以生成:

09:00–09:30
09:30–10:00

也可以生成:

09:00–09:30
09:10–09:40
09:20–09:50
09:30–10:00

不同实现方式对应不同的预约策略。


容易遗漏的情况

  • 输入未排序
  • provider 不同
  • appointment type 不同
  • 时间段不是连续的
  • 持续时间不是 10 分钟
  • ISO 时间格式错误
  • 缺少必要字段
  • 重复时间段

时间复杂度

设有效时间段数量为 n

校验:

O(n)

排序:

O(n log n)

聚合:

O(n)

总体时间复杂度:

O(n log n)

空间复杂度:

O(n)

csoahelp 提供技术面试实时文本辅助、Mock Interview、Coding、System Design 和 Behavioral Interview 辅导,帮助候选人高效准备各类软件工程师面试。

我们也有代面试,面试辅助,OA代写等服务助您早日上岸~

Leave a Reply

Your email address will not be published. Required fields are marked *