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.
1) Don't use recursion. What if we have 1,000,000 items? We'll probably get a stack overflow.
2) You don't need to keep moving the items between the stacks for insertion. In fact, for enqueue you only need one stack. You only ever need to move items from one stack to another on dequeue.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
1) Don't use recursion. What if we have 1,000,000 items? We'll probably get a stack overflow.
2) You don't need to keep moving the items between the stacks for insertion. In fact, for enqueue you only need one stack. You only ever need to move items from one stack to another on dequeue.