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.
@venkatesh23 Let's say current state is [Top->3,2,1] of Stack1. When you dequeue, we pop Stack1 to Stack2 making it's state as S1 = [] & S2 = [3,2,1->Top].
Now if you insert anything, it goes to S1 and state is
S1 = [5->Top,4] & S2 = [3,2,1->Top]
Rest is explained above.
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 →
@venkatesh23 Let's say current state is [Top->3,2,1] of Stack1. When you dequeue, we pop Stack1 to Stack2 making it's state as S1 = [] & S2 = [3,2,1->Top]. Now if you insert anything, it goes to S1 and state is S1 = [5->Top,4] & S2 = [3,2,1->Top]
Rest is explained above.