Have you ever looked at small optimizations and wondered - wow, how did they come up with that? I could never do this in an interview!
Well don't be stressed. These optimizations are useless for interviews.
"I found a neat way to avoid this loop"
"We can do this in 10 lines instead of 15"
Sound familiar?
These kind of comments rise to the top of Leetcode discussions. Why? Because people think it's cool. "The author is so smart! Why didn't I think of this before?! Surely the interviewer will be impressed by this."
No, they won't.
If it doesn't improve time/space complexity and it doesn't make the code more readable, it's unnecessary.
Many of these tricky optimizations make the code less readable. That's why they seem tricky. The interviewer will be confused. She/He might even get a bad impression because you're using cryptic code.
Here's an example. Let's say you want to reverse the elements of an array. [1,2,3,4] -> [4,3,2,1].
Coder 1 (5 lines):
int i = 0, j = a.length -1;
while (i < j) {
swap(a, i, j); // helper function
i++;
j--;
}
Coder 2 (2 lines):
for (int i = 0; i < (a.length-1)/2; i++) {
swap(a, i, a.length-1-i);
}
Many of you will think that Coder 2 is so smart. He* managed to do it in 2 lines! What a neat piece of code!
In reality, Coder 1's code is much easier to follow and much less prone to errors.
Why is Coder 2's code more prone to errors? Because he is using unnecessary math. Look at the first line - the for loop. In that line, he will need to think carefully about a couple of things:
In "i < (a.length-1)/2", should it be "<" or "<="?
Should it be "(a.length-1)/2" or "a.length/2"?
Similarly, in the second line, should it be "a.length-1-i" or "a.length-i"?
What a waste of time! Coder 1 doesn't need that. His code is more intuitive. And well, human.
Coder 1: You take 2 pointers at each end and move them inwards until they meet.
Coder 2: I can optimize this by using just one pointer! I can calculate the second element using that pointer. What a neat solution! (Gets many upvotes on Leetcode discussions)
Sadly, such optimizations get many upvotes on Leetcode. For some reason, the community thinks that complicated short code is better than longer clear code. And that gives you the impression that you need to make unnecessary optimizations. I don't think it should be called an optimization in the first place.
Most such discussions are useless. The interviewer won't care if you do it in 5 lines or 10, as long as your logic is clear, and you don't write unnecessary code.
Be careful of this. I've seen many people waste precious time appreciating these minor optimizations and feeling good about themselves. In an interview, it doesn't matter.
And this is good news for most of you. If you feel overwhelmed by the "smartness" of these tricks, breathe a sigh of relief. You don't need them.
The takeaway is - don't get overwhelmed by discussions on Leetcode. Have faith in yourself. Don't admire complex code. Interviewers prefer code that is easy to follow.
Balance Check [1]
Now, you also don't want to write too much code when less lines would do. A good rule of thumb is - follow your natural explanation.
Coder 1 followed his natural explanation:
Explanation: "You take 2 pointers at each end and move them inwards until they meet. At each step, you swap their values."
Code:
int i = 0, j = a.length -1;
while (i < j) {
swap(a, i, j); // helper function
i++;
j--;
}
Typically, your natural explanation will avoid complex code.
*I'm assuming my coders are male, just to keep the writing simple.