This pretty standard interview question. You should keep track of the current minimum when you do a push: push 1: [(v=1,m=1)] push -1: [(v=1, m=1), (v=-1, m=-1)] push 3: [(v=1, m=1), (v=-1, m=-1), (v=3, m=-1)] get_min: [(v=1, m=1), (v=-1, m=-1), (v=3, m=-1)] returns m=-1 pop: [(v=1, m=1), (v=-1, m=-1)] returns v=3 get_min: returns -1
problem is when you pop the min, you can't know which was the second to min unless you do the trick that's in the website quoted.
How do you guys come up with such an algorithm
11–20 of 21 posts
Re: How do you guys come up with such an algorithm
#12This pretty standard interview question. You should keep track of the current minimum when you do a push: push 1: [(v=1,m=1)] push -1: [(v=1, m=1), (v=-1, m=-1)] push 3: [(v=1, m=1), (v=-1, m=-1), (v=3, m=-1)] get_min: [(v=1, m=1), (v=-1, m=-1), (v=3, m=-1)] returns m=-1 pop: [(v=1, m=1), (v=-1, m=-1)] returns v=3 get_min: returns -1
Re: How do you guys come up with such an algorithm
#13Earlier quoted context omitted.
problem is when you pop the min, you can't know which was the second to min unless you do the trick that's in the website quoted.
You don't pop the min, you pop the top of the stack.
Re: How do you guys come up with such an algorithm
#14This pretty standard interview question. You should keep track of the current minimum when you do a push: push 1: [(v=1,m=1)] push -1: [(v=1, m=1), (v=-1, m=-1)] push 3: [(v=1, m=1), (v=-1, m=-1), (v=3, m=-1)] get_min: [(v=1, m=1), (v=-1, m=-1), (v=3, m=-1)] returns m=-1 pop: [(v=1, m=1), (v=-1, m=-1)] returns v=3 get_min: returns -1
Came here to say this, as far as LC questions go, this one is pretty straight forward. OP, the way you come up with a solution like this is practice and more practice, the same way you can solve an equation you never seen before, because you have a set of tools (patterns, "tricks", knowledge, ...) that allows you to come up with a solution. Keep practicing and you will see the progress.
Re: How do you guys come up with such an algorithm
#15The way you stated it the only requirement is O(1) runtime complexity on finding the min. You are already using O(n) space for the stack so you should be fine using extra linear space.
The push and pop operations also don't necessarily have to be O(1) which makes this a lot simpler. You should clarify this in the interview.
the discussion section here should give you some ideas https://leetcode.com/problems/min-stack/description/
The solution they are probably looking for is quite simple if a little tricky.
Each node in the stack should store two values (the value pushed, the min of the stack when that node was inserted)
This way you only have to compare the current value to the min at the top of the stack when inserting and don't have to find the min again when popping.
This gives O(1) findMin, push, pop and O(n) space.
Re: How do you guys come up with such an algorithm
#16Current answers are dealing with the technical aspect of the question, I'm gonna go in the other direction slightly. I've gone through a couple of leetcode style interviews recently. Basically it's either you've seen the question or have done a similar style question. I don't see how you can solve a question like that during an interview otherwise. I'm sure some people can solve things like that on the spot, but thei…
You need to grind leetcode enough that you can match the interview question to the leetcode problem.
The best case scenario is that you get the exact leetcode problem more or less. Then you put on a show pretending like you've never seen it before and act like you serendipitously arrive at the optimal solution via iterating it from a naive one.
Less than ideal, but still great is when it's not a perfect match but you can recognize the pattern required to solve it - which comes from grinding. There are a finite number of patterns and sub-patterns into which many interview questions fall into. You could still end up unlucky enough to get a question that does not, but at the end of the day, it's matter of optimizing your chances.
Re: How do you guys come up with such an algorithm
#17Re: How do you guys come up with such an algorithm
#18Re: How do you guys come up with such an algorithm
#19Current answers are dealing with the technical aspect of the question, I'm gonna go in the other direction slightly. I've gone through a couple of leetcode style interviews recently. Basically it's either you've seen the question or have done a similar style question. I don't see how you can solve a question like that during an interview otherwise. I'm sure some people can solve things like that on the spot, but thei…
Re: How do you guys come up with such an algorithm
#20They're wanting a QA role to be able to answer leetcode style questions? Why?