Range Module

A Range Module is a data structure that tracks ranges of numbers represented as half-open intervals and allows you to 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 it. If there are overlapping ranges, they should be merged seamlessly.
  • boolean queryRange(int left, int right): Returns true if every real number in [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).

(Note: For testing purposes, the input is provided as an array of commands and arguments. The output will only print the boolean results of the queryRange calls, omitting the void returns.)

Example 1:

Input: commands = ["RangeModule", "addRange", "removeRange", "queryRange"] args = [[], [10, 20], [14, 16], [10, 14]]

Output: [true]

Explanation:

addRange(10, 20) tracks all numbers from 10 to 19.99...

removeRange(14, 16) untracks numbers from 14 to 15.99... The tracked ranges are now [10, 14) and [16, 20).

queryRange(10, 14) asks if everything from 10 to 13.99... is tracked. Yes, so it returns true.

Example 2:

Input: commands = ["RangeModule", "addRange", "removeRange", "queryRange"]args = [[], [10, 20], [14, 16], [13, 15]]

Output:[false]

Explanation:

After adding [10, 20) and removing [14, 16), the numbers in [14, 15) are no longer tracked. Therefore, queryRange(13, 15) returns false.

Now Your Turn!

Pick the correct output for the given input

Input: commands = ["RangeModule", "addRange", "addRange", "addRange", "queryRange"]args = [[], [10, 14], [16, 20], [12, 18], [10, 20]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class RangeModule {
public:
RangeModule() {
}
void addRange(int left, int right) {
}
bool queryRange(int left, int right) {
}
void removeRange(int left, int right) {
}
};
 
/**
* Your RangeModule object will be instantiated and called as such:
* RangeModule* obj = new RangeModule();
* obj->addRange(left,right);
* bool param_2 = obj->queryRange(left,right);
* obj->removeRange(left,right);
*/
Test Case

Input:

Args
Commands