Accuray Interview Question

How can you make a queue container using two stacks?

Interview Answer

Anonymous

Aug 22, 2016

Label the two stacks "Inbox" and "Outbox". Put all incoming objects on the "Inbox". When a read is requested check if the Outbox is empty. If it is, move everything from the Inbox to the Outbox (which will reverse the order since you're popping and pushing). If the Outbox has stuff in it, you can read and pop from that.

1