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.
Stacks don't have indexes. The operations of a stack are: push an item onto the top, pop off an item from the top, and peek at the top item. Depending on your language, the data structure you're using might allow more operations than that, but you have to restrain yourself from using them or it makes the exercise pointless (e.g., Python lists serve as both stacks and queues, so you can trivially implement a queue using just a single "stack").
In order to add all the inbox items to the outbox, you need to be popping off the inbox items as you go, because there's no other way to access all the items. This means that there will never be previously-copied items in your inbox.
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 →
Stacks don't have indexes. The operations of a stack are: push an item onto the top, pop off an item from the top, and peek at the top item. Depending on your language, the data structure you're using might allow more operations than that, but you have to restrain yourself from using them or it makes the exercise pointless (e.g., Python lists serve as both stacks and queues, so you can trivially implement a queue using just a single "stack").
In order to add all the inbox items to the outbox, you need to be popping off the inbox items as you go, because there's no other way to access all the items. This means that there will never be previously-copied items in your inbox.