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.
Optimal pseudocode, if you resolved it but couple tests giving timout exception then, rewrite this to your preffered programming language:
enQStack=newStack()deQStack=newStack()enqueue(num):# push all new elements to enqueue stack O(1)enQStack.push(num)dequeue():# if dequeue stack has elements pop from it O(1)# else fill dequeue stack with elements from enqueue stack O(n), then pop from itifdeQStackisempty:fillDeQStack()deQStack.pop()peek():# if dequeue stack has elements peek from it O(1)# else fill dequeue stack with elements from enqueue stack O(n), then peek from itifdeQStackisempty:fillDeQStack()deQStack.peek()fillDeQStack():# fill dequeue stack with elements from enqueue stack O(n), dequeue = reversed enqueuewhileenQStackisnotempty:deQStack.push(enQStack.pop())
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
Optimal pseudocode, if you resolved it but couple tests giving timout exception then, rewrite this to your preffered programming language: