Drill

ProblemsGo › events

How many event pairs overlap

hardeventsIntervalsArraysGo

A room-booking audit counts every pair of events that clash so the worst offenders can be resolved.

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.

Solve it in Python →

Where you start

func conflictPairs(events []Event) int {
	
}

Worked examples

CallResult
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
}

The same problem in another language

More events problems in Go