This is one of the classics, and yeah, I've gotten this one too. But this one's a gimme since it's so simple and you really are just expected to have heard it. Someone at Microsoft asked me this one and I considered it a massive gift. I thought for a few moments, articulated the solution, and then wrote the code. Then I said, "of course, everyone's heard this one before; I didn't just come up with that on the spot." (you may as well wait to admit this at the end instead of the beginning since you then get an open-ended amount of time to give a stellar answer to what they asked, then you get honesty credit for admitting you knew it all along, plus you've eaten up time that they might have used to switch questions on you).
There are much worse ones on leetcode, and people expecting you to get the optimal solution are absurd. I fully expect to fail those unless I'm lucky. I don't study leetcode problems, because my interview approach is to first and foremost, implement a naive (aka slow) but correct solution in the time allotted, then engage in a conversation about ideas for improving it (not a rewrite). This has worked for me for decades, and it's also what I expect of people I'm interviewing.
This type of approach allows the interviewee to demonstrate that 1) they can write basic code, 2) they can solve a problem, 3) they can critique their own code and discuss perf implications, 4) they can think of ways to improve it even if they don't necessarily have time for a rewrite, and it allows the interviewer to demonstrate that they aren't a dick who's just playing "I'm smarter than you because I'm in the advantaged position."
Can you tell a little more about this situation? Because it sounds like you were the interviewer who expected this... Having conducted interviews at big tech companies, I can attest there is variance between interviewer expectations, despite leadership's best efforts to make it objective. So I have to wonder if you've seen it, doesn't that mean you were creating that environment?
Not only that, I had to pay for my own lunch and it took four weeks for it to finally say it wasn't hiring me.
Is the problem with leetcode the content or the process? The process is utterly broken, but any time hiring comes up it feels like most complaints are about the content.
There's the group of people for whom doing these "Algos I" problems in university was relatively painless; not that all problems were "easy" but there's an ease to approaching the problem, and with enough continuous effort the problem is solved. These people also tend to have good retention such that 10 years later, they just need to practice LC for a few weeks and they're back in top form, even if they've been building the easiest CRUD apps for years and not working their brains at all. I've seen this firsthand.
There's another group of people who, for whatever reason, are lacking the facilities to calmly and clearly approach these problems. Doing "Algos I" was a serious grind, and the Brain Toolbox for solving these problems falls apart quickly once the final has been taken. Even if doing relatively complex work at their jobs for years, when it's time to practice LC, it may take months to get anywhere near "top form".
Obviously everyone is on a spectrum between and beyond these two groups, and I fall closer to the latter.
Now add on the fact that there's a person watching you who is going to dictate whether you get a paycheck at all or a 20% boost in income. Or you're like me right now and you've been laid off. Now add on that you need to do it in 20 minutes, each minor mistake potentially becoming a devastating error if you don't catch it quickly.
I envy anyone who can just focus on the problem and remove all surrounding context. Still trying to figure out how I can add stress distractions while timing myself.
> I envy anyone who can just focus on the problem and remove all surrounding context. Still trying to figure out how I can add stress distractions while timing myself.
This is indeed a good thing to be able to do. I'm pretty good at it, but when I'm in an interview situation, I almost always have at least one moment where I start to panic a little and have to tell myself, "you know what you're doing, just relax and turn the crank." And of course, there's an implied "...but maybe you'll just choke," which I have to put out of my mind.
In other words, a huge part of being able to focus on the problem and remove the surrounding context is a sort of confidence that I can probably do fine, so I can temporarily stop worrying about how I'm looking at any exact moment.