FunctionRacerProblemsRace

Stack With a Size Limit

BeginnerClassesPython, JavaScript, TypeScript, Java, C++

Implement a class LimitedStack, a stack of integers with room for at most limit items. 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

LimitedStack(2)
push(4) returns true
push(7) returns true
push(9) returns false
is_full() returns true
pop() returns 7
peek() returns 4
is_full() returns false
pop() returns 4
pop() returns -1

With room for two items, pushing 9 onto 4 7 is refused. Popping gives 7, then 4, and the empty stack pops -1.

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