Subtract One Half-Open Interval from Another
Updated
Problem statement
You are given two finite floating-point intervals intervalA and intervalB. Each array contains exactly [start, end] and represents the half-open interval [start, end).
Return the portions of intervalA that are not covered by intervalB.
- If the intervals do not overlap, return
intervalA. - If
intervalBcompletely coversintervalA, return an empty matrix. - Otherwise, return each non-empty residual interval from left to right.
- Intervals that only touch at an endpoint do not overlap. Never return an empty interval such as
[x, x).
Function
subtractInterval(intervalA: double[], intervalB: double[]) → double[][]Examples
Example 1
intervalA = [2.5,7.5]intervalB = [4.3,9.3]return = [[2.5,4.3]]The overlap is [4.3, 7.5), so the only part of intervalA left uncovered is [2.5, 4.3).
Example 2
intervalA = [1.0,10.0]intervalB = [3.0,7.0]return = [[1.0,3.0],[7.0,10.0]]intervalB lies strictly inside intervalA, leaving one residual interval on each side. They are returned from left to right.
Example 3
intervalA = [1.0,3.0]intervalB = [3.0,5.0]return = [[1.0,3.0]]The intervals only touch at endpoint 3.0. Because they are half-open, they do not overlap, so intervalA remains unchanged.
Example 4
intervalA = [2.0,6.0]intervalB = [0.0,10.0]return = []intervalB covers every point in intervalA, so no residual interval remains.
Constraints
intervalA.length == 2andintervalB.length == 2.- Every endpoint is a finite
doublevalue. intervalA[0] < intervalA[1].intervalB[0] < intervalB[1].
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Meeting RoomsBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Merge Overlapping IntervalsAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Intervals Overlapping a Fixed-Length WindowBooking.com · ONSITE INTERVIEW
- Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions
- Online Meeting Scheduler with Fixed RoomsUber · ONSITE INTERVIEWUber interview questions
- Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
- Merge IntervalsSafe Security · ONSITE INTERVIEW
- Merge Overlapping IntervalsCapgemini · ONSITE INTERVIEWCapgemini interview questions