Determine if Two Events Have Conflict

Easy
1758.

Determine if Two Events Have Conflict

Determine if Two Events Have Conflict is an easy coding problem on arrays and strings, asked in Amazon and Google interviews.

Two events happen on the same day: event1 = [startTime1, endTime1] and event2 = [startTime2, endTime2], with every time written as "HH:MM" in 24-hour format. Both ends are inclusive.

Two events conflict when they share at least one moment. Return true if the events conflict and false otherwise.

Example 1

Input
event1 = ["09:30","10:00"], event2 = ["10:00","11:15"]
Output
true
Explanation
They touch at exactly 10:00, and both ends are inclusive.

Example 2

Input
event1 = ["08:00","09:00"], event2 = ["08:45","08:50"]
Output
true
Explanation
The second event lies inside the first.

Example 3

Input
event1 = ["13:00","14:00"], event2 = ["14:01","15:00"]
Output
false

Constraints

  • event1.length == event2.length == 2
  • event1[i].length == event2[i].length == 5
  • startTime1 <= endTime1
  • startTime2 <= endTime2
  • all times are valid 24-hour times "HH:MM"
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} event1
 * @param {string[]} event2
 * @return {boolean}
 */
var haveConflict = function(event1, event2) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Determine if Two Events Have Conflict — Problem & Solution

Determine if Two Events Have Conflict solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Determine if Two Events Have Conflict is an easy coding problem on arrays and strings, asked in Amazon and Google interviews.

  • Difficulty: Easy
  • Topics: Arrays, Strings
  • Asked at: Amazon, Google
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Two events happen on the same day: event1 = [startTime1, endTime1] and event2 = [startTime2, endTime2], with every time written as "HH:MM" in 24-hour format. Both ends are inclusive.

Two events conflict when they share at least one moment. Return true if the events conflict and false otherwise.

Example 1

Input: event1 = ["09:30","10:00"], event2 = ["10:00","11:15"]
Output: true
Explanation: They touch at exactly 10:00, and both ends are inclusive.

Example 2

Input: event1 = ["08:00","09:00"], event2 = ["08:45","08:50"]
Output: true
Explanation: The second event lies inside the first.

Example 3

Input: event1 = ["13:00","14:00"], event2 = ["14:01","15:00"]
Output: false

Constraints

  • event1.length == event2.length == 2
  • event1[i].length == event2[i].length == 5
  • startTime1 <= endTime1
  • startTime2 <= endTime2
  • all times are valid 24-hour times "HH:MM"

Hints for Determine if Two Events Have Conflict

  1. Hint 1

    It is easier to decide when two intervals do not overlap.

  2. Hint 2

    They are disjoint exactly when one ends before the other starts: end1 < start2 or end2 < start1.

  3. Hint 3

    Zero-padded "HH:MM" strings compare in the same order as the times themselves, so no parsing is needed.

How to solve Determine if Two Events Have Conflict

Two closed intervals overlap unless one finishes strictly before the other begins; fixed-width time strings can be compared directly.

Approach

  1. If endTime1 < startTime2, the first event is over before the second starts — return false.
  2. If endTime2 < startTime1, the reverse — return false.
  3. Otherwise return true.

Why it works

If neither event ends before the other starts, then max(start1, start2) <= min(end1, end2), and that moment belongs to both. Because every time has the same HH:MM width with leading zeros, lexicographic order equals chronological order.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • The ends are inclusive: events that touch at one minute conflict.
  • Checking only whether a start lies inside the other interval misses the case where one event contains the other — unless both directions are checked.

Reference solution

Python

from typing import List

def haveConflict(event1: List[str], event2: List[str]) -> bool:
    return not (event1[1] < event2[0] or event2[1] < event1[0])

Java

public static boolean haveConflict(String[] event1, String[] event2) {
    return !(event1[1].compareTo(event2[0]) < 0 || event2[1].compareTo(event1[0]) < 0);
}

C++

bool haveConflict(vector<string>& event1, vector<string>& event2) {
    return !(event1[1] < event2[0] || event2[1] < event1[0]);
}

JavaScript

var haveConflict = function(event1, event2) {
    return !(event1[1] < event2[0] || event2[1] < event1[0]);
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings