Skip to main content
LeetCode 352, Hard. Topics: Hash Table, Binary Search, Union-Find, Design, Data Stream, Ordered Set. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

Problem

Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals. Implement the SummaryRanges class:
  • SummaryRanges() Initializes the object with an empty stream.
  • void addNum(int value) Adds the integer value to the stream.
  • int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals [starti, endi]. The answer should be sorted by starti.

Examples

Constraints

  • 0 <= value <= 10<sup>4</sup>
  • At most 3 * 10<sup>4</sup> calls will be made to addNum and getIntervals.
  • At most 10<sup>2</sup> calls will be made to getIntervals.
Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026