We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This code is still perfectly functional, but a little expensive: you'll need to transfer all elements in the 'queue' twice for every enqueue.
(Enqueueing 10 and then dequeue 10 would result in ~90 elements transferred worst case.)
Using the method I suggest, you'll only need to transfer all elements in the 'queue' once, and only in the special situation (of not having a 'refreshed' old stack).
So enqueueing 10 and dequeue 10 would result in ~10 elements transferred worst case.)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queues: A Tale of Two Stacks
You are viewing a single comment's thread. Return to all comments →
This code is still perfectly functional, but a little expensive: you'll need to transfer all elements in the 'queue' twice for every enqueue. (Enqueueing 10 and then dequeue 10 would result in ~90 elements transferred worst case.)
Using the method I suggest, you'll only need to transfer all elements in the 'queue' once, and only in the special situation (of not having a 'refreshed' old stack). So enqueueing 10 and dequeue 10 would result in ~10 elements transferred worst case.)