Problem · Intervals

Range Module

Learn this problem
HardWalmart logoWalmartFULLTIMEPHONE SCREEN

Problem statement

Process a finite ordered sequence of operations for a range module. The module starts with no tracked points on the number line. The arrays operations and ranges have the same length. At step i, ranges[i] = [left, right] supplies the integer endpoints of the valid half-open interval [left, right), where left < right.

Operation Contract

  • addRange: track every point in [left, right).
  • removeRange: stop tracking every point in [left, right).
  • queryRange: produce true exactly when every point in [left, right) is currently tracked; otherwise produce false.

Apply the operations in encounter order and return a boolean[] containing the results of the queryRange operations in that same order. The update operations do not contribute values to the returned array.

Function

rangeModule(operations: String[], ranges: int[][]) → boolean[]

Examples

Example 1

operations = ["addRange","removeRange","queryRange","queryRange","queryRange"]ranges = [[10,20],[14,16],[10,14],[13,15],[16,17]]return = [true,false,true]

After adding [10,20) and removing [14,16), the module tracks [10,14) and [16,20). Therefore [10,14) is fully tracked, [13,15) crosses the removed gap, and [16,17) is fully tracked.

Example 2

operations = ["addRange","addRange","queryRange","removeRange","queryRange","queryRange","queryRange"]ranges = [[1,5],[5,8],[1,8],[3,6],[1,3],[3,6],[6,8]]return = [true,true,false,true]

The adjacent additions combine to cover [1,8). Removing [3,6) leaves [1,3) and [6,8), so only the query for the removed middle interval is false.

Example 3

operations = ["queryRange","addRange","removeRange","queryRange","addRange","queryRange","queryRange"]ranges = [[1,2],[2,4],[1,5],[2,4],[3,7],[4,6],[2,3]]return = [false,false,true,false]

The first query is false because the module is empty. The first added range is completely removed. Adding [3,7) then covers [4,6), but it does not cover [2,3).

More Walmart problems

drafts saved locally
public boolean[] rangeModule(String[] operations, int[][] ranges) {
    // Write your code here.
}
operations["addRange","removeRange","queryRange","queryRange","queryRange"]
ranges[[10,20],[14,16],[10,14],[13,15],[16,17]]
expected[true,false,true]
checking account