FunctionRacerProblemsRace

Covered Stretches of a Number Line

HardClassesPython, JavaScript, TypeScript, Java, C++

Implement a class RangeTracker that keeps track of which parts of a number line are covered. The range [left, right) holds every number x with left <= x < right, and its length is right - left. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

RangeTracker()
add(10, 20) returns 10
add(30, 40) returns 10
add(15, 35) returns 10
list_pieces() returns [[10,40]]
remove(18, 22) returns 4
list_pieces() returns [[10,18],[22,40]]
is_covered(12, 19) returns false
first_gap(15) returns 18
add(18, 22) returns 4
list_pieces() returns [[10,40]]

Adding [15, 35) covers 10 new units and joins the two pieces into [10, 40]. Removing [18, 22) splits it again, so 18 is the first number at or after 15 that is not covered. Adding [18, 22) back joins everything into one piece.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems