Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Implement Stack using Queues

Easy

Topics
StackQueueDesign

Implement a last-in-first-out (LIFO) stack using only queues. Implement MyStack with push(x), pop() (removes and returns the top element), top() (returns the top element), and empty().

Example 1

Input:  operations = ["MyStack","push","push","top","pop","empty"], values = [[],[1],[2],[],[],[]]
Output: [null,null,null,2,2,false]

Constraints

  • 1 <= x <= 9
  • At most 100 calls to push, pop, top, and empty.
  • pop and top are only called on a non-empty stack.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access