As reported: Google L3/L4 coding question (2025), "Set Difference of Floating-Point Ranges": two lists
aandbof non-overlapping floating-point intervals, sorted in ascending order; each interval is[start, end],[start, end),(start, end]or(start, end). Computea − b: the values inathat are not covered by any interval ofb, as a sorted list of non-overlapping intervals with the correct inclusion/exclusion format. Classes given:Value { isIncluded, value },Interval { start, end }andRangeUtility.removeFromList(A, B). Follow-up: what ifaandbare not sorted and may overlap internally?
Each interval has a start and an end (start < end) and, for each end, whether it is included: [ / ] mark an included end, ( / ) an excluded one. a and b are each sorted in ascending order, and the intervals within one list neither overlap nor touch.
Return a − b: the set of numbers that lie in some interval of a and in no interval of b, as a sorted list of disjoint intervals with each end correctly included or excluded. A single remaining point is written [x, x].
Example
a = [2.5, 7.5) (9.0, 10.4]
b = (4.3, 9.3) [9.5, 11.0)
-> [2.5, 4.3] [9.3, 9.5) 4.3 and 9.3 are not in b, so they stay
a = (1.0, 5.0)
b = [2.0, 3.0) (4.0, 6.0)
-> (1.0, 2.0) [3.0, 4.0]
a = [1, 5], b = [3, 5) -> [1, 3) [5, 5]
a = [1, 10], b = [1, 10) -> [10, 10]
a = [3, 5), b = [1, 7) -> (nothing left)
Constraints: 1 ≤ len(a), len(b) ≤ 10^4; 0 ≤ start < end ≤ 10^9; values have at most 2 digits after the decimal point.
Code runner format: two lines, a and b, each a space-separated list of intervals written like [2.5,7.5) (no spaces inside). Implement subtract(a, b) over Interval(start, end, startIncluded, endIncluded); the harness prints the result in the same notation, or EMPTY.
Follow-ups
- What if
aandbare not sorted and may overlap internally?