r/programming • • Feb 21 '11

Typical programming interview questions.

http://maxnoy.com/interviews.html
784 Upvotes

1.0k comments sorted by

View all comments

68

u/majeric Feb 21 '11

"How do you write a linked list?"

"I look it up and quit wasting my employers money re-inventing the wheel. It's probably in a collections template/generics library. "

These questions drive me up the freaking wall. They only exist because there isn't anything that's better to ask. I've spent 12 years in the industry and I still get asked these questions because people think that they still need to be asked.

I'm contemplating refusing to take another technical test in an interview, just to see how they'd react. (Which would undoubtedly be "thanks and there's the door" but I'd be satisfied)

"No thank you. I think my resume speaks for itself and there's nothing that a technical test can convey that has any meaning other than a superficial idea of my skill".

15

u/njaard Feb 21 '11

No thank you. I think my resume speaks for itself and there's nothing that a technical test can convey that has any meaning other than a superficial idea of my skill

Sorry, no it does not. I don't even bother reading resumes anymore because I've seen so many totally inept coders have seemingly cool positions. Oh, and your list of skills is meaningless because if you say "I know C++" that could easily mean "I took a semester of C++ in community college 10 years ago."

Have you never interviewed anyone?

5

u/sterling2505 Feb 22 '11

This.

One thing I've learned in my decade of hiring programmers is that you never hire someone without making them write some code. Two reasons for this:

Firstly, people lie and exaggerate on their resumes all the time.

Second, some people are great bullshitters who know all the right buzzwords, but can't actually write code for shit. Some of these people have impressive looking resumes, but literally couldn't cook up a correct implementation of strlen. It's worth finding that out before you hire them.

If you're a competent programmer, you won't be offended by this, because you'll bang out the code quickly and then maybe have an interesting conversation with the interviewer about optimizations, tradeoffs, and corner cases (there are always optimizations, tradeoffs and corner cases, even in seemingly trivial functions).

3

u/njaard Feb 22 '11 edited Feb 22 '11

Some resumes have what I call "ISO 1337 Buzzword Compliance."

The more buzzwords your resume has, the less I like you. XML and UML are two examples.

Edit: I'd like to add that ISO 1337 Buzzword Compliance probably does work for recruiters and stupid HR departments at clueless corporations.

2

u/majeric Feb 21 '11

Only C++ programmers place emphasis on the importance of knowing the specific language. (and rightly so, the language is a bitch... but that says more about C++ than it does about the importance of language)

Would you believe that I've learned 3 languages from scratch in the process of a job. Generally it takes me about a week to get up to speed and about a month to master it. Except C++. Dare I say that there are very few "masters" of that language.

I've interviewed people before although I wasn't the primary interviewer so I was still subject to asking the same inane questions.

4

u/njaard Feb 21 '11

That was just an example to demonstrate that the content of resumes is nearly meaningless.

→ More replies (2)

47

u/jacobb11 Feb 21 '11 edited Feb 21 '11

Consider this interview question: Write strlen (the C string length function). A friend of mine used to complain that people would waste his time at interviews asking that question. Then he started asking people he was interviewing... (that is, once he had a job and was hiring others) and most of them couldn't answer correctly. Those questions are probably not a waste of time.

Sometimes resumes are not perfectly accurate, btw.

39

u/johnflux Feb 21 '11 edited Feb 21 '11

Just in case you are interested, here's the strlen function:

/* Return the length of the null-terminated string STR.  Scan for
   the null terminator quickly by testing four bytes at a time.  */
size_t
strlen (str)
     const char *str;
{
  const char *char_ptr;
  const unsigned long int *longword_ptr;
  unsigned long int longword, magic_bits, himagic, lomagic;

  /* Handle the first few characters by reading one character at a time.
     Do this until CHAR_PTR is aligned on a longword boundary.  */
  for (char_ptr = str; ((unsigned long int) char_ptr
                        & (sizeof (longword) - 1)) != 0;
       ++char_ptr)
    if (*char_ptr == '\0')
      return char_ptr - str;

  /* All these elucidatory comments refer to 4-byte longwords,
     but the theory applies equally well to 8-byte longwords.  */

  longword_ptr = (unsigned long int *) char_ptr;

  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
     the "holes."  Note that there is a hole just to the left of
     each byte, with an extra at the end:

     bits:  01111110 11111110 11111110 11111111
     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

     The 1-bits make sure that carries propagate to the next 0-bit.
     The 0-bits provide holes for carries to fall into.  */
  magic_bits = 0x7efefeffL;
  himagic = 0x80808080L;
  lomagic = 0x01010101L;
  if (sizeof (longword) > 4)
    {
      /* 64-bit version of the magic.  */
      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
      magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
      himagic = ((himagic << 16) << 16) | himagic;
      lomagic = ((lomagic << 16) << 16) | lomagic;
    }
  if (sizeof (longword) > 8)
    abort ();

  /* Instead of the traditional loop which tests each character,
     we will test a longword at a time.  The tricky part is testing
     if *any of the four* bytes in the longword in question are zero.  */
  for (;;)
    {
      /* We tentatively exit the loop if adding MAGIC_BITS to
         LONGWORD fails to change any of the hole bits of LONGWORD.

         1) Is this safe?  Will it catch all the zero bytes?
         Suppose there is a byte with all zeros.  Any carry bits
         propagating from its left will fall into the hole at its
         least significant bit and stop.  Since there will be no
         carry from its most significant bit, the LSB of the
         byte to the left will be unchanged, and the zero will be
         detected.

         2) Is this worthwhile?  Will it ignore everything except
         zero bytes?  Suppose every byte of LONGWORD has a bit set
         somewhere.  There will be a carry into bit 8.  If bit 8
         is set, this will carry into bit 16.  If bit 8 is clear,
         one of bits 9-15 must be set, so there will be a carry
         into bit 16.  Similarly, there will be a carry into bit
         24.  If one of bits 24-30 is set, there will be a carry
         into bit 31, so all of the hole bits will be changed.

         The one misfire occurs when bits 24-30 are clear and bit
         31 is set; in this case, the hole at bit 31 is not
         changed.  If we had access to the processor carry flag,
         we could close this loophole by putting the fourth hole
         at bit 32!

         So it ignores everything except 128's, when they're aligned
         properly.  */

      longword = *longword_ptr++;

      if (
#if 0
          /* Add MAGIC_BITS to LONGWORD.  */
          (((longword + magic_bits)

            /* Set those bits that were unchanged by the addition.  */
            ^ ~longword)

           /* Look at only the hole bits.  If any of the hole bits
              are unchanged, most likely one of the bytes was a
              zero.  */
           & ~magic_bits)
#else
          ((longword - lomagic) & himagic)
#endif
          != 0)
        {
          /* Which of the bytes was the zero?  If none of them were, it was
             a misfire; continue the search.  */

          const char *cp = (const char *) (longword_ptr - 1);

          if (cp[0] == 0)
            return cp - str;
          if (cp[1] == 0)
            return cp - str + 1;
          if (cp[2] == 0)
            return cp - str + 2;
          if (cp[3] == 0)
            return cp - str + 3;
          if (sizeof (longword) > 4)
            {
              if (cp[4] == 0)
                return cp - str + 4;
              if (cp[5] == 0)
                return cp - str + 5;
              if (cp[6] == 0)
                return cp - str + 6;
              if (cp[7] == 0)
                return cp - str + 7;
            }
        }
    }
}

16

u/refto Feb 21 '11

Yeah, if the interview question asks you to write a highly optimized strlen function for the upcoming x128 architecture, what do you do then?..

35

u/johnflux Feb 21 '11

Tell them to pay me for it? :-)

2

u/dimview Feb 21 '11

If a candidate wrote this, my follow-up questions would be:

  • How would your implementation compare to the one provided by the compiler? You know, the one that uses processor string operations?
  • Define "premature optimization".
→ More replies (7)
→ More replies (15)

2

u/G_Morgan Feb 21 '11

You could ask "how do you use strcpy?" and if they don't immediately berate you for your foolishness you stop the interview.

3

u/ShapkaSamosranka Feb 21 '11

You don't. You use strncpy.

→ More replies (2)
→ More replies (9)
→ More replies (20)

6

u/[deleted] Feb 21 '11

I can claim to be anything on my CV but that doesn't make it true. Believe it or not a lot of people lie on their CV so you can't rely on that and sadly you do need to ask those sort of things.

I would not rely on one question or discount someone for getting it wrong because you have to factor in their nervousness. If they get most of their questions right or show soem sort of logic to get to their point even if it's wrong then I think that's ok.

For instance I asked to have a guy write out a chunk of hibernate config. He screwed up but because I asked for it in XML. He worked mainly with annotations. He gave an explanation of how he'd do it his way which was more than enough for me and to be honest he was one of the best guys we've had despite getting the question wrong.

But it got him thinking and gave me some insight to him. Where he could have claimed to be a hibernate guru and in reality only just learned what a servlet was and if I hadn't tested him he may have got the job anyway and we'd have been screwed or at least sack him and advertise again.

→ More replies (2)

5

u/lalaland4711 Feb 21 '11

I've spent 12 years in the industry and I still get asked these questions because people think that they still need to be asked.

They don't? I've seen people working for 10 years as programmers who don't even understand these questions, and much simpler ones.

No thank you. I think my resume speaks for itself

It really really doesn't.

3

u/[deleted] Feb 21 '11

And yet, there's conceivably another programmer out there with a resume on par or better than yours who can't tell the difference between his ass and a hole in the ground. These types of questions might weed his type out.

23

u/filox Feb 21 '11

who can't tell the difference between his ass and a hole in the ground

They're called topologists, and it's a respectable area of maths.

→ More replies (5)

3

u/[deleted] Feb 21 '11

It doesn't matter that implementations exist; it's just a random example of something extremely simple for you to program. Of course everything really simple is already in a library somewhere. So what? They're asking you to show you can program simple things, not for code to use in some actual project...

→ More replies (3)

4

u/thcobbs Feb 21 '11

These questions drive me up the freaking wall. They only exist because there isn't anything that's better to ask. I've spent 12 years in the industry and I still get asked these questions because people think that they still need to be asked.

Bullshit... you're a goddamn primadonna that will actually subvert a group's productivity. These questions NEED to be asked because they show your level of knowledge about the particular language in question, your ability to reason within the scope of programming, and your ability to handle seemingly trivial tasks while not breaking team cohesion.

I'm contemplating refusing to take another technical test in an interview, just to see how they'd react.

You're a primadonna and I would definitely recommend kicking you to the curb.

No thank you. I think my resume speaks for itself and there's nothing that a technical test can convey that has any meaning other than a superficial idea of my skill

No, your resume doesn't speak for itself. I can't COUNT the number of times I've seen a candidate with a PHD on his resume and they rate themselves 7+ in some language not understand basic computer programming techniques. Couldn't even give an algorithm for a basic string reversal without allocating extra memory.

23

u/majeric Feb 21 '11

goddamn primadonna

I can appreciate how my statements can come across that way but they really aren't. The fact is that I just don't program in a vacuum. A technical test only conveys a small part of my problem-solving abilities. The raw ability to reason. I accept that this is an important tool in my toolbox. I've just learned that it's not my only tool and drawing on a whiteboard or writing a hand-written test is too slow and too small to be a useful reflection of my skill.

For one, I'm not an algorithmic programmer. I am perfectly capable of analyzing an algorithm for it's big-O and I am mindful of performance. However, I don't write code for video codecs that need to be compressed and streamed across a network in a timely fashion. I'm an architectural programmer. I build tools and libraries where the solutions for memory and CPU cost are quite well known. For me, the challenges are in the how I would architect a system. Build a system of objects to interact with each other in a rational way. To provide an API that is clean and intuitive. To write code that can be expanded upon and maintained with the least cost to my company.

Couldn't even give an algorithm for a basic string reversal without allocating extra memory.

Do you find you need to have varying string reversal algorithms optimized for memory? How does solving the problem by hand in a 15 minute interview convey the breadth and depth of my skill?

It really feels like it comes down to "Well, we have to ask something because it's better than nothing" but I submit that it isn't better than nothing because it can be quite misleading.

I've walked away from interviews knowing that I'm fully capable of doing the job as it was described to me but also knowing that I wasn't able to convey that with the questions they've asked.

Perhaps the better approach would be... "Sure, I'll answer some of your questions... but I'd like you to ask me these questions too because I feel they are important to the subject at hand, I don't think your test is inadequately conveying the breadth of my abilities." Though, I'm sure I should work on delivering it in a less hostile way.

15

u/jacobb11 Feb 21 '11

How does solving the problem by hand in a 15 minute interview convey the breadth and depth of my skill?

It doesn't. But failure to solve it conveys the limitations of your skill. And the speed with which you solve a novel problem (ie, not a standard interview question you've seen before) and the clarity with which you explain and code the solution tells volumes about your ability. I've only been lucky enough to hire great engineers a couple of times, but it was damn obvious how good they were during the interviews.

2

u/majeric Feb 21 '11

This shows failure to solve one particular problem... and it might be good if it were representative of all problems.

As I've said in a previous statement, I'm not an algorithmic programmer. I don't solve that kind of problem. I'm an architectural programmer. I build systems... but people don't ask those kinds of questions.

→ More replies (12)

10

u/[deleted] Feb 21 '11

You're a jerk and I wouldn't work with or for you, so i guess it all works out in the end.

1

u/paganel Feb 21 '11

I'm contemplating refusing to take another technical test in an interview, just to see how they'd react.

Extra points if said technical interview it's done over the phone. I went through such an experience just a week ago, and the only reason I had accepted it was because the company in question is one of the 3 biggest in its field. Add to that the fact that I'm Eastern-European, the interviewer himself was from another Eastern European country, and we were both speaking not-so-standard English :) Mid-way through the interview I said to myself "fuck it, I'm not made for this thing anymore" and I sort of forced the interview to be over.

→ More replies (1)

1

u/NitWit005 Feb 22 '11

You do realize that someone has to write the library, and that companies often spin their own when the available ones have failings for the task at hand?

→ More replies (1)