Pregunta de entrevista de Google

Implement Queue based on a stack

Respuestas de entrevistas

Anónimo

16 jul 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.

Anónimo

16 jul 2010

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