Problem
Ask
Submissions

Problem: Data Stream as Disjoint Intervals

Medium
30 min
Explore how to implement a data stream summary that maintains disjoint intervals as numbers are added. Understand merging intervals and handling duplicates, which helps solve common interview questions on intervals and ranges.

Statement

You are given a stream of non-negative integers a1,a2,,ana_1, a_2, \dots, a_n. At any point, you need to summarize all numbers seen so far as a list of disjoint intervals.

Your task is to implement the Summary Ranges class, where:

  • Constructor: Initializes the Summary Ranges object with an empty stream.

  • Add Num(int value): Adds the integer value to the stream.

  • Get Intervals(): Returns the current summary of numbers as a list of disjoint intervals [start_i, end_i], sorted by start_i.

Note: Each number belongs to exactly one interval. Intervals must merge whenever new numbers connect or extend existing ones, and duplicate insertions should not affect the summary.

Constraints:

  • 00 \leq value 104\leq 10^4

  • At most 3×1043 \times 10^4 calls will be made to Add Num() and Get Intervals().

  • At most 10210^2 calls will be made to Get Intervals().

Problem
Ask
Submissions

Problem: Data Stream as Disjoint Intervals

Medium
30 min
Explore how to implement a data stream summary that maintains disjoint intervals as numbers are added. Understand merging intervals and handling duplicates, which helps solve common interview questions on intervals and ranges.

Statement

You are given a stream of non-negative integers a1,a2,,ana_1, a_2, \dots, a_n. At any point, you need to summarize all numbers seen so far as a list of disjoint intervals.

Your task is to implement the Summary Ranges class, where:

  • Constructor: Initializes the Summary Ranges object with an empty stream.

  • Add Num(int value): Adds the integer value to the stream.

  • Get Intervals(): Returns the current summary of numbers as a list of disjoint intervals [start_i, end_i], sorted by start_i.

Note: Each number belongs to exactly one interval. Intervals must merge whenever new numbers connect or extend existing ones, and duplicate insertions should not affect the summary.

Constraints:

  • 00 \leq value 104\leq 10^4

  • At most 3×1043 \times 10^4 calls will be made to Add Num() and Get Intervals().

  • At most 10210^2 calls will be made to Get Intervals().