Pergunta de entrevista da empresa T-Mobile

Q: How could you implement a queue with a stack?

Resposta da entrevista

Sigiloso

29 de abr. de 2021

Queues work first in, first out. Stacks work last in, first out. If you create two stacks, you can replicate the behavior of a queue by having inserted objects do the following: Check if stack 2 empty If true, enter stack 2 and process If false, add to stack 1 Repeat check if stack 2 empty If true, move stack 1 items to stack 2 If false, wait... This works because when stack 2 is empty and stack 1 is not, the order of the stack is flipped. Thus, first in stack 1 because first out of stack 2.

1