For more selected articles, search the official account:
MarkerHub

  • Author: Fang Zhihong
  • https://zhuanlan.zhihu.com/p/…

This is probably the easiest Java interview question in history.

Complete the code to determine whether an integer is odd or not:

public boolean isOdd(int i)

I believe that a considerable number of people have been ready to make fun of, as long as the “programming” people know the answer to this question and its very simple truth. Believe it or not, many of the answers I got for this pen test looked something like this:

Public Boolean isOdd(int I) {if (I % 2 == 1) {System.out.println(" isOdd "); } else {System.out.println(" is even "); }}

Then compile it and find an error. Scratch your head and change it to something like this:

public boolean isOdd(int i) { if (i % 2 == 1) { return true; } else { return false; }}

OK, I admit that I may have some problems with my ability to filter resumes, but believe it or not, there are a lot of programmers who have been working in big companies for a few years who can write code in this style.

So I went on to guide:

Me: “What type of value is required to return by the definition of this function?”

The candidate looked at the question stem: “Boolean type.”

Me: “So, what type is the value of the expression in the parentheses after your if?”

By this point, as many as 20 percent of the candidates had given up, saying they didn’t know. Well, I really don’t know how confident you are in approaching this position. But most people, if they think about it, will come up with the right answer:

Candidate: “Also a Boolean type.”

Me: “Then what?”

A few candidates didn’t say it, but I could tell they thought it was just a coincidence and didn’t know what to do next. However, most people, after thinking about it, will optimize it to the following code:

public boolean isOdd(int i) {
    return i % 2 == 1;
}

Finally passed the first level, the second level of guidance:

Me: “Then I pass in a -1?”

Nearly half of those who thought about it would say they were raised to think that only natural numbers can be odd or even, and that negative numbers don’t have odd or even. The rest of us accepted it, thought about it for a while, and changed it to this:

public boolean isOdd(int i) {
    return i % 2 == 1 || i % 2 == -1;
}

After the prompt, optimize it to look like this:

public boolean isOdd(int i) { return i % 2 ! = 0; }

Well, this is the first code implementation so far that has been compiled to fully meet the requirements. To be honest, I’d probably get by if I didn’t have any other obvious flaws from the start. I admit that my requirements are relatively low, but the interviewee can directly write such really not too many, roughly estimate, probably account for 10 to 20 percent.

But that’s not the end of it. There’s also the third and most important level:

Me: “Is there a better way?”

Candidate: “?”

Me: “I think the moulding operation is slow, is there a faster solution?”

With the exception of a few who can figure it out for themselves, the vast majority (literally) of candidates say they don’t or don’t know, and proceed to the next tip:

Me: “What’s the difference between converting an odd number and an even number to binary?”

Quite a number of candidates said that they do not understand what is binary and bit operations, some also said that Java is not C language, there is no need to study these, just like many comments will mock me as a force. A few candidates want to think, will timidly answer.

Candidate: “The last digit in an odd number is 1. The last digit in an even number is 0.”

Me: “Then what?”

The odd thing here is that most of the candidates who are up to this point will remember the shift operation, and I really don’t know why, although it does work in this case:

public boolean isOdd(int i) { return i >> 1 << 1 ! = i; }

But that’s not the point!!

Anyway, after all the guidance in the third level, there are not many people who can write the following result. I have only met two people who could write it out without any guidance at the beginning. One I took with me everywhere, and the other turned down my offer.

public boolean isOdd(int i) {
    return (i & 1) == 1;
}

Don’t think this is the end! Here comes the ultimate boss:

Me: “Is this faster than the above modulo operation?”

Candidate: “Of course, the bit operation is fast.”

Me: “But we have tested the actual code, and found that the actual running time of the above bitwise operation and modulo operation is about the same, why?”

Candidate thinks to MMP, “Are you kidding me??”

However, the person who can really answer the question, I have not met in the interview process, it may be that Daniel did not think of the company I work for. Only in a certain company, a colleague thought about it and gave me the right answer. Is it that the companies I’ve worked for are too low?…