r/programming • u/svpino • May 08 '15
Five programming problems every Software Engineer should be able to solve in less than 1 hour
https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k
Upvotes
8
u/serados May 08 '15
Maintain a "Queue Stack" and a "Dequeue Stack".
On queue operation:
Push element into "Queue Stack"
On dequeue operation:
If "Dequeue Stack" is not empty, pop "Dequeue Stack"
Else, pop and push every element from the "Queue Stack" into the "Dequeue Stack" - O(N) and pop the "Dequeue Stack"
Informally, every element is at most pushed twice and popped twice over (once each into the "Queue" and "Dequeue" stacks) and pushes and pops are O(1), making queues/dequeues amortized O(1).