Pergunta de entrevista da empresa Meta

Implement stack using a queue

Respostas da entrevista

Sigiloso

31 de mar. de 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

Sigiloso

28 de abr. de 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.

Sigiloso

19 de nov. de 2010

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