FastPrepTarget Interval Conflict

Target Interval Conflict

Google logoGoogle● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given a target half-open interval [target[0], target[1]) and an unsorted collection of half-open intervals intervals. Return true if the target conflicts with at least one interval. Otherwise, return false.

Two half-open intervals conflict when they share at least one point. Endpoints that only touch are not a conflict. Equivalently, [aStart, aEnd) and [bStart, bEnd) do not conflict when aEnd <= bStart or bEnd <= aStart.

This is a single query. The input does not need to be sorted.

Function

hasIntervalConflict(target: int[], intervals: int[][]) → boolean

Examples

Example 1

target = [4,8]intervals = [[8,10],[1,4],[6,7]]return = true

The intervals [1,4) and [8,10) only touch the target at an endpoint, but [6,7) shares points with [4,8).

Example 2

target = [4,8]intervals = [[10,12],[1,4],[8,10]]return = false

Every interval is disjoint from the target. In particular, [1,4) ends when the target begins, and [8,10) begins when the target ends.

Constraints

  • target.length == 2.
  • Every row of intervals has length 2.
  • Every target and collection interval is valid: start < end.
  • intervals may be empty and may appear in any order.

More Google problems

See Google hiring insights
public boolean hasIntervalConflict(int[] target, int[][] intervals) {
  // Write your code here.
}
target[4,8]
intervals[[8,10],[1,4],[6,7]]
expectedtrue
Checking account…