FunctionRacerProblemsRace

Set That Picks in Turn

MediumClassesPython, JavaScript, TypeScript, Java, C++

Implement a class RotatingSet, a set of integers whose pick method takes turns through the members in increasing order. Write these methods:

The last pick is remembered as a value, so it still counts after that value has been removed from the set. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

RotatingSet()
insert(5) returns true
insert(2) returns true
insert(9) returns true
insert(5) returns false
pick() returns 2
pick() returns 5
pick() returns 9
pick() returns 2
remove(5) returns true
pick() returns 9
remove(9) returns true
pick() returns 2
count_members() returns 1

The picks go 2, 5, 9 and wrap back to 2. After 5 is removed the next member above 2 is 9. After 9 is removed nothing is above 9, so the pick wraps to 2.

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