← Questions
Earliest common slot in two calendars
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's coding round, last asked Sep 2025. It tests intervals, two pointers and sorting.
Given the calendars of two people and a meeting length d in minutes, return the earliest start time of a window of at least d minutes in which both are free. Each calendar lists busy times, and each person also has working hours; the meeting must fall inside both people's working hours. Then generalize to N people.
Interface
Unless you agree on a different interface with the interviewer, assume:
earliest_slot(calendars: list[list[list[int]]], working_hours: list[list[int]], d: int) -> int-
calendars[i]is personi's busy intervals[start, end), sorted and non-overlapping, in minutes.working_hours[i]is[ws, we). Return the earliesttsuch that[t, t + d)lies inside every person's working hours and overlaps nobody's busy time, or-1.
Example
calendars = [[[60, 120], [180, 240]], # person A busy
[[0, 90], [150, 200]]] # person B busy
working_hours = [[0, 480], [30, 480]]
earliest_slot(calendars, working_hours, 30) -> 120 # both free on [120, 150)
earliest_slot(calendars, working_hours, 31) -> 240 # [120, 150) is too short; next free from 240
earliest_slot([[[0, 480]]], [[0, 480]], 30) -> -1
Constraints
- Intervals are half-open: busy
[a, b)followed by busy[b, c)leaves no free time atb. - An empty calendar means free for the whole working window.
- Up to 10^6 intervals in total in the large follow-up.
Clarifying questions
- Are the intervals busy times or free times? Sorted? Half-open?
- Is the meeting at least
dor exactlydminutes? - Do working hours repeat every day, or is one window given?
- Are times in one time zone?
What a strong answer covers
- Intersecting working hours into one common window
W = [max(ws), min(we)), and returning -1 at once if it is shorter thand. - Merging all busy intervals into one sorted timeline (a two-pointer merge for two people, a heap for
N), then scanning the gaps insideWfor the first of length>= d, including the gap after the last busy block. - Half-open handling: touching blocks merge, and a zero-length gap is never a slot.
- Cost: O(M) for two sorted lists, O(M log N) for
Nlists with a heap, whereMis the total number of intervals. - Tests: no busy time, back-to-back meetings, a gap exactly
dlong, disjoint working hours.
Follow-ups
-
Npeople: what changes in the algorithm and its cost? - People are in different time zones with working hours per local day. How do you normalize and handle daylight saving time?
- Calendars are highly fragmented, with 10^6 intervals. How do you stay fast?
- Return only slots after the current time without taking
nowas a parameter. How do you inject a clock and keep tests deterministic? - Calendars are updated while searches run. How do you keep reads consistent: locks, copy-on-write or versioned snapshots?
Where it's asked
- Uber · Coding · Last asked Sep 202560 min · Two rounds; one is often object-oriented design (a parking lot, a meeting scheduler, an expression evaluator) · No AI, except a newer AI-assisted debugging round in some senior loops
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
