How many event pairs overlap
A room-booking audit counts every pair of events that clash so the worst offenders can be resolved.
- Every event has a half-open interval [start, end).
- A pair overlaps when the two intervals share at least one point.
- Count each unordered pair once.
conflictPairs(events: list<Event>) → int
Go 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
func conflictPairs(events []Event) int {
}
Worked examples
| Call | Result |
|---|---|
conflictPairs([]Event{Event{Start: 1, End: 5}, Event{Start: 3, End: 8}, Event{Start: 10, End: 15}}) | 1 |
conflictPairs([]Event{Event{Start: 1, End: 5}, Event{Start: 6, End: 10}}) | 0 |
conflictPairs([]Event{Event{Start: 1, End: 5}, Event{Start: 2, End: 4}, Event{Start: 3, End: 8}}) | 3 |
conflictPairs([]Event{Event{Start: 1, End: 10}, Event{Start: 2, End: 3}, Event{Start: 4, End: 5}, Event{Start: 6, End: 7}}) | 3 |
Hint
A brute-force double loop over all i < j pairs and the overlap test from bookings-overlap.
Reference solution in Go
func conflictPairs(events []Event) int {
count := 0
for i := 0; i < len(events); i++ {
for j := i + 1; j < len(events); j++ {
if events[i].Start < events[j].End && events[j].Start < events[i].End {
count++
}
}
}
return count
}