Why top() function is O(n) in this problem?

Isnt it O(1) ? please explain

@devanshkapur, since the queue follow FIFO (first in first out) principle and stack follows LIFO (Last In First Out) so , the element that would have been present in the stack at the top will be present at the last place in the queue , as push operation in queue will append the element at last of queue, so if Q1 size is n, you need to move n-1 elements in the second queue i.e Q2 and print the top value from Q1 , this will take o(n) operation

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.