It's a fun problem to solve, and can be done by maintaining the 2 stacks. Since stack stores the newest element on top of the stack, while doing pop() on Queue, it should be last element to pop out. For this we can maintain another stack and fill it by removing element from the first stack. Note now the order of removing element from the other stack will be same as Queue