Sometimes, it's a way to elegantly solve some problems. Imagine you have a binary tree and you need to find it's depth. The answer is the maximum depth of its left and right subtree plus one. This easily translates into a recursive code.
It's true that it's rarely used in production quality code, but nonetheless it's sometimes useful and you should have a good command of writing recursive code.
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
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.
Yes. Of course. But what do you do when the top of the stack IS the min? Your new min should change, but you won't have the information to update to the second min unless you do the trick in the article.
it can be used to build better statistical modelling and computational tools that might, in the future, enable scientists and researchers to more efficiently or effectively figure out statistical or causal relationships -- and in some niche applications, e.g. in pharma or finance or insurance or advertising, that information might be valuable and help some people get rich. but the people able to capture the most value from those developments are unlikely to be the researchers or the people building the software tools.