Question d’entretien chez Meta

Implement stack using a queue

Réponses aux questions d'entretien

Utilisateur anonyme

31 mars 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

Utilisateur anonyme

28 avr. 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.

Utilisateur anonyme

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.