Problems › JavaScript › 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.
shiftConflicts(slots: list<Slot>) → int
Where you start
function shiftConflicts(slots) {
}
Worked examples
| Call | Result |
|---|---|
shiftConflicts([{"start":0,"end":100},{"start":10,"end":90},{"start":0,"end":100}]) | 3 |
shiftConflicts([{"start":0,"end":100},{"start":100,"end":200}]) | 0 |
shiftConflicts([]) | 0 |
shiftConflicts([{"start":5,"end":5},{"start":0,"end":10}]) | 1 |
Hint
For each request scan all the others for one strictly broader window.
Reference solution in JavaScript
function shiftConflicts(slots) {
let n = 0;
for (let i = 0; i < slots.length; i++) {
for (let j = 0; j < slots.length; j++) {
if (i !== j && slots[j].start <= slots[i].start && slots[j].end >= slots[i].end) { n++; break; }
}
}
return n;
}