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
C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
int shiftConflicts(std::vector<Slot> slots) {
}
Worked examples
| Call | Result |
|---|---|
shiftConflicts(std::vector<Slot>{Slot{0, 100}, Slot{10, 90}, Slot{0, 100}}) | 3 |
shiftConflicts(std::vector<Slot>{Slot{0, 100}, Slot{100, 200}}) | 0 |
shiftConflicts(std::vector<Slot>{}) | 0 |
shiftConflicts(std::vector<Slot>{Slot{5, 5}, Slot{0, 10}}) | 1 |
Hint
For each request scan all the others for one strictly broader window.
Reference solution in C++
int shiftConflicts(std::vector<Slot> slots) {
int n = 0;
for (size_t i = 0; i < slots.size(); i++) {
auto& a = slots[i];
bool hit = false;
for (size_t j = 0; j < slots.size(); j++) {
if (i == j) continue;
auto& b = slots[j];
if (b.start <= a.start && b.end >= a.end) { hit = true; break; }
}
if (hit) n++;
}
return n;
}