Question d’entretien chez Amazon

Implementing a queue with stack as the internal datastructure

Réponses aux questions d'entretien

Utilisateur anonyme

6 sept. 2011

use two stacks

1

Utilisateur anonyme

7 mars 2014

Keep 2 stacks, let's call them inbox and outbox. Queue: - Push the new element onto inbox Dequeue: - If outbox is empty, refill it by popping each element from inbox and pushing it onto outbox - Pop and return the top element from outbox Using this method, each element will be in each stack exactly once - meaning each element will be pushed twice and popped twice, giving amortized constant time operations.