Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Time complexity

US

Jul 5, 2024

I have a question regarding time complexity. Does using while loop not considered a nested iteration of the given input?

If yes then the time-complexity should be O(n^2).

1

0

Comments
Comments
Shubham Vora
Shubham Voraa year ago

The time complexity of the stack solution is actually O(n), not O(n^2).

Here's why: Each element is pushed and popped from the stack at most once. Although it might seem like popping multiple elements could lead to O(n^2) complexity, each element only enters and...

On this page