Skip to content

Queue From Stacks

EasyPython 3Stacks

Implement a FIFO queue using only two stacks. The only operations you may perform on the internal stacks are push (list.append), pop (list.pop) on the last element, and emptiness/length checks — no indexing into the middle, no insert(0, ...).

Implement the class below:
  - enqueue(value): add value to the back of the queue.
  - dequeue(): remove and return the value at the front of the queue;
    raise IndexError if the queue is empty.
  - size(): number of elements currently in the queue.

Each element should be moved between the stacks at most once, making both operations amortized O(1).

Examples:
  q = QueueFromStacks()
  q.enqueue(1); q.enqueue(2); q.enqueue(3)
  q.dequeue() == 1
  q.enqueue(4)
  q.dequeue() == 2 ; q.dequeue() == 3 ; q.dequeue() == 4

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…