Domanda di colloquio di Meta

Implement stack using a queue

Risposte di colloquio

Anonimo

31 mar 2011

I think the question is to implement a stack using a queue DS. For every push(), enqueu() in the queue. Then dequeu() every element and enqueue() the same immediately until we reach the element which was recently enqueued. This way we maintain the queue in the form expected by the stack (LIFO)

1

Anonimo

28 apr 2011

deveffort, i guess you will have to maintain 2 queues. After the first dequeue, your queue will contain items that are needed for stack retrieval(LIFO). But any more items you want to add to this queue will mess it up unless you maintain another queue. So retrievals are always from one queue and additions area always on a different queue.

Anonimo

19 nov 2010

You need two stacks. You push to stack A, and pop from stack B. When B is empty, you reverse A onto B.