Question d’entretien chez Google

Implement Queue based on a stack

Réponses aux questions d'entretien

Utilisateur anonyme

16 juil. 2010

Do insertions need to be the same running time? Off the top of my head, I would say once insertions to the queue are finished, pop everything off of stack A and push onto stack B. To remove from the queue, pop from stack B. To insert to the queue, pop everything off of stack B, push onto A, push the new insertion onto A, then move everything back to B.

Utilisateur anonyme

16 juil. 2010

That means insertions are no longer O(1), but O(n).