DEV Community


Discussion on: Implementing Queue with 2 Stacks

alainvanhout profile image
Alain Van Hout

Isn't dequeue O(n) here?

saurabhpro profile image
Saurabh Kumar Author

A very good point.

If you consider the worse case where we add and remove alternatively, we perform 2 operations (one push and then one pop)
AFAIK The problem of O(n) lies with the use of ArrayList where the remove() takes O(n) to find the index.
This post was supposed to give a starting point for how the concept is done.

Can you walk through your process - maybe there's a more efficient way without adding complex logic?