Potential error in implementing MinStack

Hi everyone, I think there might be a potential error in the solution of implementing the MinStack given by Mosh.

The line of code ‘item < minStack.peek()’ should actually be ‘item <= minStack.peek()’.

Try this example: [5,2,10,2,1]. Print the minimum and pop and print minimum one by one.

Using the first code, it gives ‘1,2,5,5,5’. Using the second code, it gives the right answer ‘1,2,2,2,5’.

Answered this one in the Java thread already:

2 Likes