Summarize a Stream of Integers as Sorted Disjoint Intervals

Quick Overview

Design a class that takes a stream of non-negative integers and returns the numbers seen so far as sorted, disjoint intervals. It tests keeping an ordered structure correct under inserts, handling every merge case between neighboring intervals, and complexity trade-offs when intervals stay few.

Summarize a Stream of Integers as Sorted Disjoint Intervals

Company: Oracle

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Other

Given a data stream of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals. Implement the `SummaryRanges` class, which initializes the object with an empty stream, supports adding integers to the data stream, and returns a summary of the integers in the stream as a list of disjoint intervals `[start_i, end_i]` sorted by `start_i`. Consecutive integers belong to the same interval: after adding 1 and 2, the summary is `[[1, 2]]`. Implement the following methods: - `SummaryRanges()`: Initializes the object with an empty stream. - `addNum(value: int)`: Adds the integer `value` to the data stream. - `getIntervals() -> List[List[int]]`: Returns a summary of the integers currently in the stream as a list of disjoint intervals `[start_i, end_i]`, sorted by `start_i`. Example 1: ```text Input: ["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"] [[], [1], [], [3], [], [7], [], [2], [], [6], []] Output: [null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]] Explanation: sr = SummaryRanges() # empty stream sr.addNum(1) # stream: {1} sr.getIntervals() # → [[1, 1]] sr.addNum(3) # stream: {1, 3}; 1 and 3 are not consecutive sr.getIntervals() # → [[1, 1], [3, 3]] sr.addNum(7) # stream: {1, 3, 7} sr.getIntervals() # → [[1, 1], [3, 3], [7, 7]] sr.addNum(2) # stream: {1, 2, 3, 7}; 2 joins [1, 1] and [3, 3] sr.getIntervals() # → [[1, 3], [7, 7]] sr.addNum(6) # stream: {1, 2, 3, 6, 7}; 6 extends [7, 7] to [6, 7] sr.getIntervals() # → [[1, 3], [6, 7]] ``` Example 2: ```text Input: ["SummaryRanges", "addNum", "addNum", "addNum", "getIntervals"] [[], [1], [2], [3], []] Output: [null, null, null, null, [[1, 3]]] Explanation: sr = SummaryRanges() # initialize empty stream sr.addNum(1) # stream: {1} sr.addNum(2) # stream: {1, 2}, extends [1,1] to [1,2] sr.addNum(3) # stream: {1, 2, 3}, extends [1,2] to [1,3] sr.getIntervals() # → [[1, 3]] (all consecutive, single interval) ``` ```hint Where a new value can land If the intervals stay sorted, a new value can only touch the interval just before it and the interval just after it. Find those two without walking the whole list. ``` ```hint Cases to check Write down every way a value can relate to those two neighbors, including a value the summary already covers, and trace each one on a small input before coding. ``` ### Constraints & Assumptions - `0 <= value <= 10⁴` - At most `3 * 10⁴` calls in total will be made to `addNum` and `getIntervals`. ### Clarifying Questions to Ask - Can the same value be added more than once, and should a repeat leave the summary unchanged? - Which call is more frequent: `addNum` with only occasional `getIntervals`, or the reverse? - May `getIntervals` hand back the internal list, or must the caller get a copy it can modify safely? ### What a Strong Answer Covers - The structure that keeps intervals sorted and disjoint, and how the neighbors of a new value are located. - Every case for a new value, including values already covered and a value that closes a gap between two intervals. - The invariant that holds after each call, and why each case preserves it. - Time and space for `addNum` and `getIntervals`, in terms of the number of intervals and the number of calls. - How the design changes when intervals stay few while the stream grows large. ### Follow-up Questions - What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream? - How would you change the design if `getIntervals` were called far more often than `addNum`, or far less often? - How would you support removing a value from the stream?

Overview: Design a class that takes a stream of non-negative integers and returns the numbers seen so far as sorted, disjoint intervals. It tests keeping an ordered structure correct under inserts, handling every merge case between neighboring intervals, and complexity trade-offs when intervals stay few.

Oracle logo
Oracle
Nov 24, 2026
hardSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Given a data stream of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.

Implement the SummaryRanges class, which initializes the object with an empty stream, supports adding integers to the data stream, and returns a summary of the integers in the stream as a list of disjoint intervals [start_i, end_i] sorted by start_i. Consecutive integers belong to the same interval: after adding 1 and 2, the summary is [[1, 2]].

Implement the following methods:

  • SummaryRanges() : Initializes the object with an empty stream.
  • addNum(value: int) : Adds the integer value to the data stream.
  • getIntervals() -> List[List[int]] : Returns a summary of the integers currently in the stream as a list of disjoint intervals [start_i, end_i] , sorted by start_i .

Example 1:

Input:
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]

Output: [null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]

Explanation:
sr = SummaryRanges()     # empty stream
sr.addNum(1)             # stream: {1}
sr.getIntervals()        # → [[1, 1]]
sr.addNum(3)             # stream: {1, 3}; 1 and 3 are not consecutive
sr.getIntervals()        # → [[1, 1], [3, 3]]
sr.addNum(7)             # stream: {1, 3, 7}
sr.getIntervals()        # → [[1, 1], [3, 3], [7, 7]]
sr.addNum(2)             # stream: {1, 2, 3, 7}; 2 joins [1, 1] and [3, 3]
sr.getIntervals()        # → [[1, 3], [7, 7]]
sr.addNum(6)             # stream: {1, 2, 3, 6, 7}; 6 extends [7, 7] to [6, 7]
sr.getIntervals()        # → [[1, 3], [6, 7]]

Example 2:

Input:
["SummaryRanges", "addNum", "addNum", "addNum", "getIntervals"]
[[],              [1],      [2],      [3],      []]

Output: [null, null, null, null, [[1, 3]]]

Explanation:
sr = SummaryRanges()             # initialize empty stream
sr.addNum(1)                     # stream: {1}
sr.addNum(2)                     # stream: {1, 2}, extends [1,1] to [1,2]
sr.addNum(3)                     # stream: {1, 2, 3}, extends [1,2] to [1,3]
sr.getIntervals()                # → [[1, 3]]    (all consecutive, single interval)

Constraints & Assumptions

  • 0 <= value <= 10⁴
  • At most 3 * 10⁴ calls in total will be made to addNum and getIntervals .

Clarifying Questions to Ask Guidance

  • Can the same value be added more than once, and should a repeat leave the summary unchanged?
  • Which call is more frequent: addNum with only occasional getIntervals , or the reverse?
  • May getIntervals hand back the internal list, or must the caller get a copy it can modify safely?

What a Strong Answer Covers Guidance

  • The structure that keeps intervals sorted and disjoint, and how the neighbors of a new value are located.
  • Every case for a new value, including values already covered and a value that closes a gap between two intervals.
  • The invariant that holds after each call, and why each case preserves it.
  • Time and space for addNum and getIntervals , in terms of the number of intervals and the number of calls.
  • How the design changes when intervals stay few while the stream grows large.

Follow-up Questions Guidance

  • What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?
  • How would you change the design if getIntervals were called far more often than addNum , or far less often?
  • How would you support removing a value from the stream?