Implement a Range Module with Add, Query, and Remove

Quick Overview

Implement a range tracker that adds, removes, and queries half-open integer intervals while preserving coverage across overlaps and adjacent endpoints. Explore interval invariants, splitting and merging updates, ordered search, deterministic query results, and boundary-focused tests.

Implement a Range Module with Add, Query, and Remove

Company: Walmart Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Process Range Tracking Operations Implement `processRanges(operations) -> queryResults`. The tracker is initially empty. Process every operation from left to right. Each operation is a three-element JSON-compatible array: ```text [type, left, right] ``` - `type` is exactly `"add"`, `"remove"`, or `"query"`. - `left` and `right` are integers with `1 <= left < right <= 1,000,000,000`. - Every range is half-open: `[left, right)` includes `left` and excludes `right`. Operation semantics: - `add` starts tracking every number in `[left, right)`. Already tracked portions remain tracked. - `remove` stops tracking every number in `[left, right)`. Tracked portions outside that range remain tracked. - `query` asks whether every number in `[left, right)` is currently tracked. Return one Boolean for each `query` operation, in the same order those queries appear. `add` and `remove` operations do not add anything to the result. ### Constraints - `0 <= operations.length <= 10,000`. - Every operation has exactly three elements and satisfies the encoding and endpoint rules above. - Adjacent tracked ranges have no gap because `[a, b)` and `[b, c)` together cover `[a, c)`. - The input must not be mutated. - Aim for logarithmic boundary search plus work proportional to the stored ranges changed by an update. ```hint Test the endpoints Before choosing a representation, trace cases where ranges are disjoint, touch at one endpoint, overlap, contain one another, or are split by removal. ``` ### Examples ```text operations = [ ["add", 10, 20], ["remove", 14, 16], ["query", 10, 14], ["query", 13, 15], ["query", 16, 17] ] queryResults = [true, false, true] ``` ```text operations = [ ["query", 1, 2], ["add", 5, 10], ["add", 10, 12], ["query", 5, 12], ["remove", 3, 20], ["query", 5, 6] ] queryResults = [false, true, false] ```

Quick Answer: Implement a range tracker that adds, removes, and queries half-open integer intervals while preserving coverage across overlaps and adjacent endpoints. Explore interval invariants, splitting and merging updates, ordered search, deterministic query results, and boundary-focused tests.

|Home/Coding & Algorithms/Walmart Labs
Walmart Labs logo
Walmart Labs
Jul 16, 2026, 12:00 AM
hardSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Process Range Tracking Operations

Implement processRanges(operations) -> queryResults.

The tracker is initially empty. Process every operation from left to right. Each operation is a three-element JSON-compatible array:

[type, left, right]
  • type is exactly "add" , "remove" , or "query" .
  • left and right are integers with 1 <= left < right <= 1,000,000,000 .
  • Every range is half-open: [left, right) includes left and excludes right .

Operation semantics:

  • add starts tracking every number in [left, right) . Already tracked portions remain tracked.
  • remove stops tracking every number in [left, right) . Tracked portions outside that range remain tracked.
  • query asks whether every number in [left, right) is currently tracked.

Return one Boolean for each query operation, in the same order those queries appear. add and remove operations do not add anything to the result.

Constraints

  • 0 <= operations.length <= 10,000 .
  • Every operation has exactly three elements and satisfies the encoding and endpoint rules above.
  • Adjacent tracked ranges have no gap because [a, b) and [b, c) together cover [a, c) .
  • The input must not be mutated.
  • Aim for logarithmic boundary search plus work proportional to the stored ranges changed by an update.

Examples

operations = [
  ["add", 10, 20],
  ["remove", 14, 16],
  ["query", 10, 14],
  ["query", 13, 15],
  ["query", 16, 17]
]

queryResults = [true, false, true]
operations = [
  ["query", 1, 2],
  ["add", 5, 10],
  ["add", 10, 12],
  ["query", 5, 12],
  ["remove", 3, 20],
  ["query", 5, 6]
]

queryResults = [false, true, false]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...