Range Module - Problem

A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as half-open intervals and query about them.

A half-open interval [left, right) denotes all the real numbers x where left <= x < right.

Implement the RangeModule class:

  • RangeModule() Initializes the object of the data structure.
  • void addRange(int left, int right) Adds the half-open interval [left, right), tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval [left, right) that are not already tracked.
  • boolean queryRange(int left, int right) Returns true if every real number in the interval [left, right) is currently being tracked, and false otherwise.
  • void removeRange(int left, int right) Stops tracking every real number currently being tracked in the half-open interval [left, right).

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["RangeModule", "addRange", "removeRange", "queryRange", "queryRange"], params = [[], [10, 20], [14, 16], [10, 14], [13, 15]]
Output: [null, null, null, true, false]
💡 Note: RangeModule initializes empty. addRange(10, 20) tracks [10, 20). removeRange(14, 16) removes [14, 16), leaving [10, 14) and [16, 20). queryRange(10, 14) returns true (fully covered). queryRange(13, 15) returns false (15 not tracked).
Example 2 — Overlapping Ranges
$ Input: operations = ["RangeModule", "addRange", "addRange", "queryRange"], params = [[], [10, 15], [12, 18], [10, 18]]
Output: [null, null, null, true]
💡 Note: addRange(10, 15) tracks [10, 15). addRange(12, 18) merges with existing to track [10, 18). queryRange(10, 18) returns true (fully covered).
Example 3 — Edge Case Empty Query
$ Input: operations = ["RangeModule", "queryRange"], params = [[], [1, 5]]
Output: [null, false]
💡 Note: Empty RangeModule has no tracked ranges, so queryRange(1, 5) returns false.

Constraints

  • 1 ≤ left < right ≤ 109
  • At most 104 calls will be made to addRange, queryRange, and removeRange.

Visualization

Tap to expand
Range Module - Interval Merging INPUT Number Line [0-25] 0 10 14 16 20 Operations: 1. RangeModule() 2. addRange(10, 20) 3. removeRange(14, 16) 4. queryRange(10, 14) 5. queryRange(13, 15) Half-open interval [a, b) Includes a, excludes b params: [[], [10,20], [14,16], [10,14], [13,15]] ALGORITHM STEPS 1 Initialize Empty interval list: [] 2 addRange(10, 20) Merge overlapping intervals [10, 20) 3 removeRange(14, 16) Split interval, remove middle [10,14) [16,20) 4 queryRange(10, 14) [10,14) in [10,14)? TRUE true 5 queryRange(13, 15) [13,15) spans gap! FALSE false Final: [[10,14), [16,20)] 10 14 16 20 FINAL RESULT Tracked Intervals: [10,14) gap [16,20) 10 14 16 20 Output Array: [null, null, null, true, false] Result Breakdown: RangeModule() --> null (init) addRange(10,20) --> null removeRange(14,16) --> null query(10,14) OK query(13,15) NO All operations completed! Key Insight: Interval Merging maintains a sorted list of non-overlapping intervals. When adding a range, merge all overlapping intervals. When removing, split affected intervals. Query checks if the requested range is fully contained within a single tracked interval (no gaps allowed). TutorialsPoint - Range Module | Interval Merging Approach
Asked in
Google 15 Facebook 12 Amazon 8
32.1K Views
Medium Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen