We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Determine if Two Events Have Conflict

Number: 2536

Difficulty: Easy

Paid? No

Companies: Google, Goldman Sachs


Problem Description

Given two events that occur on the same day defined by their start and end times in "HH:MM" format, determine if the two events have any time conflict. A conflict exists if the events share at least one common time point.


Key Insights

  • The events are inclusive, meaning if one event ends when the other begins, it is considered a conflict.
  • Since times are in "HH:MM" format and are zero-padded, they can be compared as strings or converted to minutes.
  • Checking for an overlap can be done by ensuring one event starts before the other event ends and vice versa.

Space and Time Complexity

Time Complexity: O(1) – The operations are constant time comparisons. Space Complexity: O(1) – Only a few variables are used regardless of input size.


Solution

The main idea is to check whether the two events overlap. Since the event times are given in "HH:MM" format, we can either compare them as strings or convert them into an integer representation (minutes since midnight) for clarity. The events conflict if:

  • The start time of event1 is less than or equal to the end time of event2, and
  • The start time of event2 is less than or equal to the end time of event1.

This approach uses simple condition checks with constant time operations.


Code Solutions

# Function to check for event conflicts
def have_conflict(event1, event2):
    # Convert "HH:MM" to minutes since midnight
    def time_to_minutes(time_str):
        hours, minutes = map(int, time_str.split(":"))
        return hours * 60 + minutes
    
    # Convert event1 and event2 times to minutes
    start1 = time_to_minutes(event1[0])
    end1 = time_to_minutes(event1[1])
    start2 = time_to_minutes(event2[0])
    end2 = time_to_minutes(event2[1])
    
    # Check if events overlap (inclusive of boundaries)
    return start1 <= end2 and start2 <= end1

# Example usage:
print(have_conflict(["01:15","02:00"], ["02:00","03:00"]))  # Output: True
← Back to All Questions