FastPrepSubtract One Half-Open Interval from Another
Problems/Subtract One Half-Open Interval from Another

Subtract One Half-Open Interval from Another

Google logoGoogle● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the goal of A minus B, half-open endpoints, the case analysis, and a worked example with floating-point numbers. We added the container type and a fixed left-to-right output. Our version is about 97% the same as the reported question.

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 intervalB completely covers intervalA, 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 == 2 and intervalB.length == 2.
  • Every endpoint is a finite double value.
  • 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

Interview reports that mention this question

  • Google Intern Grid and Tree Rounds: 7 Experiences (Part 2 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public double[][] subtractInterval(double[] intervalA, double[] intervalB) {
      // Write your code here.
    }
}
Checking account…