Problems › Python › scheduling
How many people try to book the same slot
Requests come in as start/end minutes. Count how many requests are fully contained inside another request — a same-slot double booking.
- A request is contained when another request starts no later and ends no earlier.
- Two identical requests each contain the other; count every one that is contained, so both are flagged.
- A request never contains itself.
shift_conflicts(slots: list<Slot>) → int
Where you start
def shift_conflicts(slots: list[Slot]) -> int:
Worked examples
| Call | Result |
|---|---|
shift_conflicts([Slot(start=0, end=100), Slot(start=10, end=90), Slot(start=0, end=100)]) | 3 |
shift_conflicts([Slot(start=0, end=100), Slot(start=100, end=200)]) | 0 |
shift_conflicts([]) | 0 |
shift_conflicts([Slot(start=5, end=5), Slot(start=0, end=10)]) | 1 |
Hint
For each request scan all the others for one strictly broader window.
Reference solution in Python
def shift_conflicts(slots: list[Slot]) -> int:
n = 0
for i, a in enumerate(slots):
if any(j != i and b.start <= a.start and b.end >= a.end for j, b in enumerate(slots)):
n += 1
return n