A more "natural" sulution would be to sort the list first, and then look for consecutive array items where the difference is not 1.
A more "natural" sulution would be to sort the list first, and then look for consecutive array items where the difference is not 1.
I think questions like this are fun and novel and also have absolutely no place in an interview for software developers.
Edit: the slightest of arguments for a question like this is can they find novel runtime optimizations. A work sample test is better. Grab some crap code from the codebase like a double forloop that should have been a map look up and have them make it more performant.
Unless they can swear up and down that what they actually do at this company, for at least a significant part of the day, is throw each other against the wall (er umm, whiteboard) and insist that they solve problems like these (and do so elegantly) within 10 minutes or less...
... or get fired ...
... then what I'll be looking for is simply -- the door.
I was asked this exact question at my on-campus interview with one of the Big Five software companies for a Program Manager internshi between my 3rd and 4th years of college.
I had done no “leetcode grinding”-style prep.
Subtract from 5050? No, I didn’t think of that. But I did think of summing the numbers 1..100 with a for loop, sum the numbers in the input array, and subtract.
Not the perfect solution. But it’s O(n).
And I did get the internship.
My point being not that I’m some kind of algorithmic prodigy: I’m very much not. But if a college kid can solve it without any prep, nor rote memorization as GP suggests...