Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Stack Using Queues

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Stack Using Queues. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Unity Editor records actions in LIFO order for an undo-style command history. Implement this stack behavior using exactly one queue and queue operations only.

Write process_stack_operations(operations). Each operation is one of:

  • ["push", value]: Add integer value to the top of the stack.
  • ["pop"]: Remove and return the top value.
  • ["top"]: Return the top value without removing it.
  • ["empty"]: Return whether the stack contains no values.

Return an array containing outputs for every pop, top, and empty operation, in order. push operations produce no output. You may assume pop and top are never called on an empty stack.

A Python collections.deque may be used as the queue, but access it only through append, popleft, and len.

Constraints

  • 1 <= operations.length <= 10^4
  • Each push value is an integer in [-10^9, 10^9]
  • pop and top are called only when the simulated stack is nonempty
  • Use exactly one queue and queue operations only

Function Signature

def process_stack_operations(operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output