The Stack Protocol (LIFO)
EasyAcc. 98.5%
+15 XP 5
The Undo Mechanism (LIFO)
A Stack is a fundamental data structure modeled after a real-world stack of plates. It follows the LIFO (Last-In, First-Out) principle: the last item you place on the stack is the very first one you take off. This is exactly how the "Undo" button in your text editor works!
The Assignment
Your mission is to simulate a basic stack processor. Your function receives an array of commands and an array of values.
- Initialize an empty array to serve as your internal stack.
- Iterate through the
commandsarray:- If the command is "PUSH", add the corresponding value from
values[i]to the top of the stack. - If the command is "POP", remove the top element and print it.
- If the command is "PEEK", print the top element without removing it.
- If the command is "PUSH", add the corresponding value from
01EXAMPLE 1
Input
commands=["PUSH", "PUSH", "POP"], values=[10, 20, 0]Output
20Explanation: 20 was pushed last, so it pops first.
Constraints
- Use standard array methods (`push`, `pop`).
Data StructuresStack
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
commands["PUSH","PUSH","POP"]
values[10,20,0]
Expected Output
20
Click RUN to test your solution