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.
Are you copying/moving items from the input stack to the output stack only when it is empty? Or are you copying/moving it every time you call peek/pop? If you are trying to make sure that one of your stacks is always complete and in correct queue order, then you're doing extra work that doesn't need to be done. Queues output oldest values first, so your second/output stack only needs to be refilled if it is empty.
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 →
Are you copying/moving items from the input stack to the output stack only when it is empty? Or are you copying/moving it every time you call peek/pop? If you are trying to make sure that one of your stacks is always complete and in correct queue order, then you're doing extra work that doesn't need to be done. Queues output oldest values first, so your second/output stack only needs to be refilled if it is empty.