You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where:
event1 = [startTime1, endTime1] andevent2 = [startTime2, endTime2].Event times are valid 24 hours format in the form of HH:MM.
A conflict happens when two events have some non-empty intersection (i.e., some moment is common to both events).
Return true if there is a conflict between two events. Otherwise, return false.
Example 1:
Input: event1 = ["01:15","02:00"], event2 = ["02:00","03:00"] Output: true Explanation: The two events intersect at time 2:00.
Example 2:
Input: event1 = ["01:00","02:00"], event2 = ["01:20","03:00"] Output: true Explanation: The two events intersect starting from 01:20 to 02:00.
Example 3:
Input: event1 = ["10:00","11:00"], event2 = ["14:00","15:00"] Output: false Explanation: The two events do not intersect.
Constraints:
event1.length == event2.length == 2event1[i].length == event2[i].length == 5startTime1 <= endTime1startTime2 <= endTime2HH:MM format.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to checking for event conflicts involves examining all possible relationships between the two events' time intervals. We consider every scenario of how the events might overlap or not overlap. If any overlap exists then there is a conflict.
Here's how the algorithm would work step-by-step:
def have_conflict(event_one_start_time, event_one_end_time,\
event_two_start_time, event_two_end_time): # Check if the first event starts before the second event ends.
if event_one_start_time <= event_two_end_time:
#Check if the second event starts before the first event ends.
if event_two_start_time <= event_one_end_time:
#If both are true, there is a conflict
return True
#If the above conditions are false, there is no conflict.
return FalseTo determine if two events conflict, we can check if they overlap in time. Instead of comparing every possible time within the events, we only need to check the start and end times of each event against the other.
Here's how the algorithm would work step-by-step:
def do_events_conflict(event_one_start, event_one_end, event_two_start, event_two_end):
# Check if event one starts before event two ends
if event_one_start < event_two_end:
# Check if event two starts before event one ends
if event_two_start < event_one_end:
# If both checks are true, then the events overlap
return True
# If either check is false, there is no conflict
return False| Case | How to Handle |
|---|---|
| Null or undefined input for either event time array | Throw an IllegalArgumentException or return false/error code indicating invalid input |
| Event 1 ends before it starts (startTime > endTime) | Return true immediately, because if start time is AFTER end time, there will always be overlap. |
| Event 2 ends before it starts (startTime > endTime) | Return true immediately, because if start time is AFTER end time, there will always be overlap. |
| Events are exactly adjacent, one ending at the exact moment the other starts (no overlap) | Ensure the comparison uses strict inequality (< and >) rather than less than or equal (<=) to avoid false positives |
| Event times are very large integers leading to potential overflow in calculations | Use long or BigInteger data types where appropriate, or check for overflow before returning a result |
| One event completely contains the other event | This scenario should be handled correctly by the general overlap check, but it's worth testing specifically |
| Start and end times for both events are exactly the same | The algorithm should correctly identify this as an overlapping scenario, returning true |
| All event times are zero or the same value (startTime == endTime for both events) | This should be considered as overlapping, and the function should return true |