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.
Yeah! It takes O(n) time for the worst case you mentioned. It's my mistake. But the good thing is that the next (n-1) dequeue operations will take O(1) time. I actually divided that O(n) time uniformly over all the 'n' elements considering that they are going to be dequeued sooner or later. So, one could see that although the worst case happens to be O(n) but only once for every 'n' elements.
BTW, thanks for pointing it out.
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 →
Yeah! It takes O(n) time for the worst case you mentioned. It's my mistake. But the good thing is that the next (n-1) dequeue operations will take O(1) time. I actually divided that O(n) time uniformly over all the 'n' elements considering that they are going to be dequeued sooner or later. So, one could see that although the worst case happens to be O(n) but only once for every 'n' elements. BTW, thanks for pointing it out.