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.
you need 2 stacks to make a queue, one covers the push operation and one covers the pops and tops. The pushing stack is what the queue looks like from the back of the line the pop/top stack is looking at the queue from the front of the line. if your queue was 3,4,5 the pusher sees 3,4,5 and the poper sees 5,4,3. The trick is the queue is virtual and there only needs to be 1 copy of each number across the two stacks so if the pop/top stack has 5,4,3 then the push stack would be empty. New items go on the stack that is the back of the line while they come off the stack that is the front of the line
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 →
you need 2 stacks to make a queue, one covers the push operation and one covers the pops and tops. The pushing stack is what the queue looks like from the back of the line the pop/top stack is looking at the queue from the front of the line. if your queue was 3,4,5 the pusher sees 3,4,5 and the poper sees 5,4,3. The trick is the queue is virtual and there only needs to be 1 copy of each number across the two stacks so if the pop/top stack has 5,4,3 then the push stack would be empty. New items go on the stack that is the back of the line while they come off the stack that is the front of the line