A calendar app shows a warning when someone has accepted two meetings that run at the same time. Each meeting is a pair [start, end] of numbers with start < end. A meeting occupies the time from start up to, but not including, end, so a meeting ending at 10 and one starting at 10 do not clash.
Write two functions that take the array meetings, in any order:
canAttendAll(meetings) returns true if no two meetings overlap, so one person can attend every one of them, and false otherwise.
firstClash(meetings) finds the moment the person is first double-booked. Order the meetings by start time, breaking equal starts by their position in the input, and walk through that order. Return the input indexes [i, j] of the first two neighbours in that order that overlap, the earlier one first, or null if there is no clash.
Rules:
An empty list and a single meeting have no clash: true and null.
Two meetings with the same start always clash.
If meetings is not an array of [start, end] pairs of finite numbers, throw a TypeError. If a meeting has start >= end, throw a RangeError.
Do not change the input array or its pairs.
Lists can hold 200,000 meetings, so comparing every pair will not finish in time. Aim for O(n log n).
canAttendAll([[0, 30], [5, 10], [15, 20]]); // falsefirstClash([[0, 30], [5, 10], [15, 20]]); // [0, 1]: [5, 10] starts while [0, 30] runscanAttendAll([[7, 10], [2, 4]]); // truecanAttendAll([[9, 10], [10, 11]]); // true: back to back is finefirstClash([[10, 20], [0, 5], [18, 25], [4, 6]]); // [1, 3]: [4, 6] starts before [0, 5] ends
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
2
code listings
5
edge cases
What's inside
The full problem, with examples
Tests that check your solution
Step-by-step
Approach
Solution
Complexity
Edge cases
Follow-up questions
Card worldwide
UPI and net banking in India
One-time payment, nothing renews
Private, and saved in this browser as you type. All your notes are on My lists.