v9.1.1
Menu

Implement Stack using Queues

easyPROStacks & QueuesMicrosoft DSA

Implement a LIFO stack using only two queues. Support push, pop, top, and empty.

Output results of pop, top, and empty operations.

Example

Input
push,1;push,2;top;pop;empty
Output
2
2
False
Reasoning
  • We start with an empty stack and perform the given operations in sequence: push,1 and push,2, resulting in a stack with elements 1 and 2.
  • The top operation returns the last element added to the stack, which is 2.
  • The pop operation removes the last element added to the stack, which is also 2, leaving the stack with only element 1.
  • The empty operation checks if the stack is empty, which it is not, since it still contains element 1, so it returns False.

Constraints

  • 1 <= val <= 9
  • At most 100 operations

Learn this in

More from Microsoft DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.