KatInterview

Pair Lab Visits with Follow-Up Consultations from 10-Minute Slots

Greedy & IntervalsAuto-graded · 19 test cases
MediumAuto-gradedGreedy & Intervalsintervalsmergingbinary-search

Last updated

Validate and merge 10-minute availability slots, expand them into 30-minute visits, and list every lab-then-consultation pairing with a wait of 0 to 60 minutes.

Lab-then-Consultation Booking

A clinic sells a combined visit made of two appointments: a "lab" appointment first, then a "consultation" appointment later the same day. Providers publish their availability as many small 10-minute slots. Your job is to turn those slots into real 30-minute appointments and list every valid lab/consultation pairing.

Input

slots is a list of dictionaries. Each has the shape

{"provider_id": str, "type": str, "start": "HH:MM", "end": "HH:MM"}

Times use the 24-hour clock.

Processing rules

1. Keep only usable slots. A slot is usable only if all of the following hold:

  • type is exactly "lab" or "consultation".
  • Both start and end have a minute part that is a multiple of 10 (00, 10, 20, 30, 40, 50).
  • end is exactly 10 minutes after start. A slot that crosses midnight, such as 23:50 to 00:00, is therefore not usable.

Skip every other record (including records with missing fields or malformed values). If the same provider lists the identical slot with the same type more than once, count it only once.

2. Merge slots. Within each (provider_id, type) group, join slots that overlap or touch end-to-start into maximal continuous windows. For example, 09:00-09:10 and 09:10-09:20 become 09:00-09:20.

3. Create 30-minute appointments. In every merged window, create each appointment that is exactly 30 minutes long, starts on a 10-minute step, and lies entirely inside the window. A window 09:00-09:40 gives 09:00-09:30 and 09:10-09:40. A window shorter than 30 minutes gives nothing. Appointments can never span a gap between two windows.

4. Pair appointments. Form a pair for every lab appointment L and every consultation appointment C where

0 <= C.start - L.end <= 60   (in minutes)

Both bounds are inclusive. The two appointments may belong to the same provider if that provider offers both types. wait_minutes is C.start - L.end.

Output

Return a list of dictionaries, one per pair, shaped like

{
  "lab":          {"provider_id": str, "start": "HH:MM", "end": "HH:MM"},
  "consultation": {"provider_id": str, "start": "HH:MM", "end": "HH:MM"},
  "wait_minutes": int
}

Times are zero-padded HH:MM strings. Sort the list by these keys, in order:

  1. lab start time
  2. consultation start time
  3. lab provider_id (ordinary string comparison)
  4. consultation provider_id

If no pair exists, return an empty list.

Constraints

  • 0 <= len(slots) <= 2000
  • All times lie within a single day (00:00 to 23:59).
  • Time strings are intended to be in HH:MM form, but invalid records must be ignored as described above.
  • The number of output pairs may be large, so avoid comparing every lab against every consultation when you can locate the matching consultations directly.

Examples

Example 1

Input: build_visit_pairs([{'provider_id': 'P1', 'type': 'lab', 'start': '09:00', 'end': '09:10'}, {'provider_id': 'P1', 'type': 'lab', 'start': '09:10', 'end': '09:20'}, {'provider_id': 'P1', 'type': 'lab', 'start': '09:20', 'end': '09:30'}, {'provider_id': 'P1', 'type': 'lab', 'start': '09:30', 'end': '09:40'}, {'provider_id': 'P2', 'type': 'consultation', 'start': '10:00', 'end': '10:10'}, {'provider_id': 'P2', 'type': 'consultation', 'start': '10:10', 'end': '10:20'}, {'provider_id': 'P2', 'type': 'consultation', 'start': '10:20', 'end': '10:30'}])

Output: [{'lab': {'provider_id': 'P1', 'start': '09:00', 'end': '09:30'}, 'consultation': {'provider_id': 'P2', 'start': '10:00', 'end': '10:30'}, 'wait_minutes': 30}, {'lab': {'provider_id': 'P1', 'start': '09:10', 'end': '09:40'}, 'consultation': {'provider_id': 'P2', 'start': '10:00', 'end': '10:30'}, 'wait_minutes': 20}]

Example 2

Input: build_visit_pairs([{'provider_id': 'A', 'type': 'lab', 'start': '08:00', 'end': '08:10'}, {'provider_id': 'A', 'type': 'lab', 'start': '08:20', 'end': '08:30'}, {'provider_id': 'B', 'type': 'consultation', 'start': '09:00', 'end': '09:10'}, {'provider_id': 'B', 'type': 'consultation', 'start': '09:10', 'end': '09:20'}, {'provider_id': 'B', 'type': 'consultation', 'start': '09:20', 'end': '09:30'}])

Output: []

Example 3

Input: build_visit_pairs([{'provider_id': 'P', 'type': 'lab', 'start': '12:00', 'end': '12:10'}, {'provider_id': 'P', 'type': 'lab', 'start': '12:00', 'end': '12:10'}, {'provider_id': 'P', 'type': 'lab', 'start': '12:10', 'end': '12:20'}, {'provider_id': 'Q', 'type': 'consultation', 'start': '13:00', 'end': '13:10'}, {'provider_id': 'Q', 'type': 'consultation', 'start': '13:10', 'end': '13:20'}, {'provider_id': 'Q', 'type': 'consultation', 'start': '13:20', 'end': '13:30'}])

Output: []

Hints

Hint 1

Convert every time to minutes since midnight right away, and do all comparisons and arithmetic on integers rather than strings.

Hint 2

Collect valid slots in a set per (provider, type) to remove duplicates, then sort and sweep to merge. Treat a slot that starts exactly where the current window ends as part of that window.

Hint 3

Sort all consultation appointments by start time. For each lab ending at time e, the matching consultations are those with start in [e, e + 60], which form one contiguous range you can find with two binary searches.