Subtract One Half-Open Interval from Another
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's September 2026 report is an interval subtraction problem that looks too easy to fail. Two half-open intervals, return what's left of A after cutting out B. It's array work with a few comparisons, no data structure at all. The trap is the edge case that breaks a naive solution: touching endpoints and zero-width leftovers. If you blank on the case split during the OA, StealthCoder is the invisible safety net that reads the problem and hands you a clean solution. But this one is small enough to own tonight.
The problem
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].
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to think in terms of what survives, not what overlaps. The left piece is [A.start, min(A.end, B.start)). The right piece is [max(A.start, B.end), A.end). Keep each piece only if its start is strictly less than its end. That one check handles everything. No overlap on the left of A gives a right piece equal to A, and the left piece collapses. Touching at 3.0 in Example 3 gives a left piece [1.0,3.0) and a right piece [5.0... wait, max(1,5)=5 which is greater than 3, so it's dropped. Full cover drops both. The common pitfall is checking overlap with <= instead of <, or returning [x, x). Another is returning A twice when there's no overlap. Compare with strict inequalities only, and don't do arithmetic on the doubles. StealthCoder is the hedge if the live OA rattles you and the case split slips.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Subtract One Half-Open Interval from Another cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Subtract One Half-Open Interval from Another FAQ
What's the trick to Subtract One Half-Open Interval from Another?+
Build the left piece as [A.start, min(A.end, B.start)) and the right piece as [max(A.start, B.end), A.end). Keep each one only if start is strictly less than end. That covers no overlap, partial overlap, containment, and full cover without separate branches.
How do touching endpoints work in this problem?+
They don't overlap, because the intervals are half-open. If A is [1,3) and B is [3,5), the left piece is [1,3) and the right piece has start 5 and end 3, so it's dropped. You get A back unchanged. Strict inequality is what makes this fall out naturally.
How hard is this one really for a Google OA?+
Easy on algorithm, annoying on edge cases. There's no sorting or data structure. Candidates lose points by returning empty intervals like [x, x) or mishandling the touching case. Write the four examples as tests before you submit and you're fine.
Do I need to worry about floating-point precision?+
Not here. You only compare and pass through the given endpoint values using min and max. You never add or subtract them, so no epsilon is needed. Use strict less-than and return the original values exactly as given.
How do I prepare for this in 48 hours?+
Practice the pattern of computing surviving pieces with min and max, then filtering on start < end. Run it by hand on all four examples plus the no-overlap case on each side. Twenty minutes of that beats grinding random interval problems.