FunctionRacerProblemsRace

Tray of Weights

HardClassesPython, JavaScript, TypeScript, Java, C++

Implement a class WeightTray for a stack of weights on a tray: weights are put on top of each other, and usually the top one is taken off first. But sometimes the heaviest weight is pulled out from wherever it sits. 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

WeightTray()
put(5)
put(2)
put(5)
take_heaviest() returns 5
read_top() returns 2
get_heaviest() returns 5
take_heaviest() returns 5
take_top() returns 2
take_top() returns -1

The tray holds 5, 2, 5 from the bottom up. Taking the heaviest removes the upper 5, which leaves 2 on top. The next heaviest is the other 5, and after that only the 2 is left.

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