Remember seeing these discussions on Leetcode?
"Java Solution, beats 98.85%"
"4ms solution in C++"
Yea, they get a lot of upvotes. And they're a complete waste of time.
Do you really think the interviewer cares about how many milliseconds your code is going to take?
It's a useless metric even in tech companies. No one is measuring the runtime of your solution. On top of that, it's highly language dependent. Java solutions will naturally be slower than C.
Interviewers care about the time/space complexity. It's what affects performance the most. Few milliseconds here and there don't matter.
In fact, at top tech companies, you can trade off milliseconds for readability. Many of these "best" performing solutions are not very readable.
Ask any interviewer, they would rather have well-structured readable code than something that saves 2ms of runtime.
Most of you probably know this already. But some might be shocked. You might actually consider your faster timed solution better. The worst part is, you might even try to justify this during an interview. Please don't fall for it.
Measuring runtime is a relic of competitive programming. Interviews are not competitive programming.
Don't waste your valuable time on even looking at this.
And most of all, don't feel disappointed if your solution is at the bottom 10% of solutions. It doesn't matter in interviews.
Balance Check [1]
There is no balance check for this one. Just ignore runtime, it is 100% useless for interviews.
You should only care about the time and space complexity. And of course, you don't want to do things like make infinite loops. But any reasonable runtime is good enough. So just avoid thinking about it.
Here's a bigger side-effect of this: often, when you run recursive functions on Leetcode, it shows "Time Limit Exceeded". Read about it in this article.