It is very revealing to analyze the diverse responses that you can get to a simple FizzBuzz-like question. That's what I'm going to share here.

How would you find the largest number from a predefined array of integers?

Why this particular question?

The primary demographic of the interviews that I take are in India. This particular problem is something that they definitely learn in the first semester of their university course, regardless of which stream they're from. Everyone from Printing & Packaging Technology engineers to Electronics and Computer Science, they all learn it in the first year.

The second reason is that I want to test if you can accurately form logical thoughts, and then condense them into code. Logically, do you understand initialization, the invariant? Even if you don't know those terms (which you should if you're from computer science), can you still see and discover those concepts for yourself?

Do note that all these candidates have at least 2 to 3 years of professional experience post college. Also, in start-ups, you generally have to pull your own weight and wear multiple hats.

Why did I say predefined array?

The first thing developers do is write code to take the input array from the user. Why? Because that's how they've been taught in college, and they'll usually spend about 15m to just write code to take user input.

How do the responses look like

Over the years, I've got wildly different responses, and each of them indicates something about the candidate. We should always be careful as to not generalize too much, though.

Writing half of a bubble sort

This is how the code looks like:

var array = [...predefined...];
var max_number = 0;

for (var i = 0; i < array.length - 1; i++) {
    if (array[i] > array[i+1]) {
        max_number = array[i];
    }
}

I'm pretty sure you'd be wondering what exactly is the above code solving, if anything at all?

My theory is that it looks a lot like the first inner loop for bubble sort, but with a few key differences. Instead of swapping, the person is assigning the value to the max_number variable.

I could still digest the fact that you could bubble the maximum number up to the first index and then return it. But the code above makes no sense, and it never looks at the last element of the array.

The reason I call this writing "half of a bubble sort" is because I think that's where it comes from. These are people who know that sorting is too much, but all they can do is borrow ideas from existing code and copy them without understanding the underlying concepts.

Asking if they can sort the array and find the largest number

At this point I can safely start assuming that though it says "Computer Engineering" in their educational qualifications, they don't fully understand the fundamentals of computing.

I do counter by asking them to find a more efficient way, but I'm not entirely sure how they'd quantify efficiency. They choose the more convenient route all the time, regardless of what is right.

Either they do decide that they need to find an efficient way and try writing a linear time algorithm, or they give up. At that point, I give up too.

Claiming that sorting is more efficient than a linear algorithm

A lot of times, when I'd ask why didn't they go with a linear algorithm, they'd say that it involves a lot of "comparisons," and hence would be slower. With sorting, it's just a function call to the language's sort function.

Though such scenarios seem entirely improbable, but it has happened to me a few times, which is very surprising. Measuring the computational complexity of something through its visual complexity are signs of a lazy programmer. They will always prefer their own convenience first.

Sure, Bill Gates would prefer a lazy person for a difficult job. And that advice might sound okay for writing a new piece of software. But it's really advice for maintaining and refactoring code.

Beginning to write their own sorting algorithm

My eyes widen every time I see a nested for loop as a solution for this problem.

It's difficult to believe that a person who'd understand sorting would not understand a linear scan, which leads us to believe that they don't even understand the sorting algorithm that they just wrote. If you think that too, then you'd be right, and I'll tell you why.

The "nested loops" that the write, in fact, is the bubble sort algorithm. Why bubble sort out of all the others? Because that's what is taught to first year university students (or at least was while I was a student). Again, we see the propensity towards convenience rather than thinking for oneself.

Ignoring the negative integers case

Most of the people who are able to write the complete code often miss the negative case:

var array = [...predefined...]
var max_number = 0

for (var i = 0; i < array.length; i++) {
    if (array[i] > max_number) {
        max_number = array[i];
    }
}

Forgive the JavaScript-esque pseudo-code. By initializing the max_number variable to 0, you'd ensured that you'll never get the right answer for an all negative array. For an input like [-1,-2,-3], instead of -1, you'd get 0 as the answer.

Usually, at this point, I try to nudge the candidate into checking the validity of their code. I ask them if they can think of any case where it might not work.

Again, there are two types of candidates here. Ones who figure it out, and ones who don't. The ones who don't would be very bad at debugging anything of significance, and can waste a lot of time of other developers and seniors.

Stubbornly ignoring the negative integers case

I've once had a candidate argue with me that the problem statement said nothing about negative numbers. And he continued arguing even after I pointed out that the original problem statement mentioned integers, and by definition, it included negative numbers too.

Such candidates might hardly take responsibility for any of their actions, and might put the blame on someone else the first instance they get. It's okay to make mistakes, but not admitting them means you'll never learn, and it's always going to be someone else's fault.

Conclusion

Of course, a few do get it. It's not just about a person's technical knowledge, but also about his or her will to do the right thing, rather than something that's convenient.

Reminds me of Rich Hickey's Simple Made Easy talk.