Range Module

HardIntervalsdesign

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Track a set of real numbers made up of half-open ranges. A range [left, right) contains every real x with left <= x < right. Implement: - RangeModule() starts with nothing tracked. - addRange(left, right) starts tracking every number in [left, right). Parts already tracked stay tracked. - queryRange(left, right) returns true if **every** number in [left, right) is currently tracked, otherwise false. - removeRange(left, right) stops tracking every number in [left, right) that is currently tracked. Ranges that only touch count as continuous: after adding [1, 5) and [5, 8), queryRange(2, 7) is true. Keep the tracked set as sorted, disjoint intervals so each operation costs O(log n) plus the number of intervals it merges or splits.

Examples

Input: ["RangeModule","addRange","removeRange","queryRange","queryRange","queryRange","addRange","queryRange"] [[],[10,20],[14,16],[10,14],[13,15],[16,17],[14,16],[10,20]]
Output: [null,null,null,true,false,true,null,true]
Removing [14,16) leaves [10,14) and [16,20). [13,15) crosses the gap. Adding [14,16) back closes the gap, so [10,20) is fully tracked.
Input: ["RangeModule","queryRange","addRange","addRange","queryRange","removeRange","queryRange"] [[],[1,2],[1,5],[5,8],[2,7],[1,8],[4,5]]
Output: [null,false,null,null,true,null,false]
Nothing is tracked at first. [1,5) and [5,8) touch, so [2,7) is covered. Removing [1,8) clears everything.

Constraints

  • 1 <= left < right <= 10^9
  • At most 10^4 calls in total.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More design problems