dsa · easy
Stack from Queues
Implement a last-in-first-out stack. Internally you may only use FIFO queues (append at the back, pop from the front). Construct with MyStack(). Then support push(x), pop(), top(), and empty().
pop() and top() are never called on an empty stack except through empty() itself.
Fill in MyStack; the starter walks ops / args for you.
Methods
MyStack()create an empty stackpush(x)pushxonto the stack; returns nothingpop()remove and return the top valuetop()return the top value without removing itempty()trueif the stack holds nothing
Example
`` MyStack() empty push(1) [1] 1 is top push(2) [1, 2] 2 is top top() → 2 stack unchanged pop() → 2 [1] remains empty() → false ``
Fill in the MyStack class. The starter already walks ops / args and calls your methods — leave the driver at the bottom as-is. Constructors contribute null; booleans print as true / false.
Constraints
Calls <= 100 1 <= x <= 10^9
Examples
Example 1
Input: ["MyStack","push","push","top","pop","empty"] [[],[1],[2],[],[],[]] Expected: [null,null,null,2,2,false]