DEV Community

Discussion on: Implementing Queue with 2 Stacks

Collapse
 
saurabhpro profile image
Saurabh Kumar

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?