FunctionRacerProblemsRace

Last In, First Out

BeginnerStacks and queuesPython, JavaScript, TypeScript, Java, C++

The numbers of pushed are put on an empty stack one after another, so the last one ends up on top. Then the top item is taken off, pops times in a row.

Return the items that were taken off, in the order they came off. If pops is larger than the number of items, the stack simply runs empty and everything comes off.

Example 1

pushed [1,2,3,4]
pops 2
returns [4,3]

4 went on last, so it comes off first, and then 3.

Example 2

pushed [5,6]
pops 5
returns [6,5]

Five pops are asked for but only two items are there: 6 comes off, then 5.

Example 3

pushed [7,8,9]
pops 0
returns []

Nothing is taken off.

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 Stacks and queues problems