Pregunta de entrevista de Meta

Implement a stack using only queue(s)

Respuestas de entrevistas

Anónimo

30 may 2012

If you pop everything from one q into another it will still be FIFO though.. my solution is: have 2 queues, primary q and secondary q. to push object onto stack, you push it into primary q. to pop object from stack, you pop every element in the primary q except the last one (which is the most recent element pushed) and as you pop each element, you push that element into the secondary q. now the primary q has nothing but the most recent element pushed (the one to be popped by the stack) and the secondary queue has everything the primary queue had minus the element to be popped off the stack. now you designate the secondary queue as the primary queue, and the original primary queue (which is now the new secondary queue) is cleared.

1

Anónimo

22 may 2012

you need to use 2 push everything into one and pop() it into the other - reversing it basically