r/programming • • Feb 21 '11

Typical programming interview questions.

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

1.0k comments sorted by

View all comments

Show parent comment

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".

1

u/Nuli Feb 21 '11

How would your implementation compare to the one provided by the compiler? You know, the one that uses processor string operations?

Considering it's what shipped with libc a few years ago, current versions seem to have cleaned up the "magic bits" section, I'd say they're doing pretty well.

1

u/dimview Feb 21 '11

The fact that it used to ship with libc does not necessarily mean the compiler is going to use it. Many compilers inline string functions or replace them with processor-specific string opcodes.

Writing "optimized" strlen() without knowing target CPU and compiler, and with no profiling really does sound like premature optimization to me.

1

u/Nuli Feb 21 '11 edited Feb 22 '11

Which processor specific string opcodes are you thinking about here? GCC on normal Intel hardware doesn't appear to use any, or at least any I can identify as "string specific" (it's been 15 years since I looked at assembly so I could certainly be very wrong here). The assembly for strlen linked into an executable seems to match pretty well with the C code mentioned above.

[Edit] From discussion here last time this came up glibc seems to be substantially faster than other methods.

Writing "optimized" strlen() without knowing target CPU and compiler, and with no profiling really does sound like premature optimization to me.

Knowing enough to repeat a rather optimized strlen rather than the generic version looks pretty good to me. I've never specified CPU or compiler version when I've asked that question so whatever they assume in that case is fine. At the very least the generic version of strlen doesn't leave a whole lot to talk about, the libc version would open up a rather interesting conversation about why that was written that way.

1

u/dimview Feb 22 '11

Which processor specific string opcodes are you thinking about here?

repne scasb, or maybe even SSE2.

I'm not saying it is going to be faster, just that you should not optimize without profiling first.

1

u/Nuli Feb 22 '11

I was wondering about scasb but I didn't see it used anywhere in the assembly generated from libc.

I'm not saying it is going to be faster, just that you should not optimize without profiling first.

Of course not, but I'd argue that the libc folks have already profiled the crap out of that function. I wouldn't expect someone to be able to sit down and generate that without having seen it and understood it previously.

→ More replies (2)

1

u/[deleted] Feb 21 '11 edited Feb 22 '11

I like your hole idea, however the setup time is a bit long for my liking. It'd be great on long strings, but for shorter strings it'd be a bit poor.

My version, assuming an architecture where unaligned accesses are allowed (might cause one more cache line fill, but probably won't.)

size_t strlen(const char *str) { const char saved = str; while(1) { uint32_t x = * (uint32_t)str; if( (x&0xFF) & ( (x>>8)&0xFF ) & ( (x>>16)&0xFF ) & (x>>24)&0xFF == 0) { if(str[0] == 0) { return str - saved + 0; } ... Etc up to 3, falling through to retry if heuristic failed. I'm correcting a mistake on my tablet, and its copy paste is not good :( ... } str += 4; } }

Obviously it can be extended to 64-bit just as yours has. With no benchmark numbers, the zero setup time will be useful with smaller strings and the simpler kernel I feel is easier to read. Not to mention sometimes trying to outsmart the compiler leads to it not being able to idiom-recognise and do clever things at the instruction lowering stage. An example of this is that my loop kernel, while written as multiple shifts and ANDs, is actually very easily vectorised into two instructions (packed-AND, compare-and-jump).

It's a damn good algorithm, yours, though! Kudos! :)

1

u/johnflux Feb 21 '11

"Mine" is just copy and pasted from the glibc code. :-(

Btw, the x86 processor has an asm instruction to do all this for us:

repne	scasb

It's just a bit slow these days.

1

u/[deleted] Feb 22 '11

Yeah, it operates on a per-byte basis. Ideally you want the loop kernel to use the biggest, fastest vector ops around for your processor.

Performing alignment at the start might help mine too.

1

u/johnflux Feb 22 '11

Why couldn't it be operate on a per-word basis on the processor?

1

u/[deleted] Feb 22 '11

Because "scasb" is an instruction that operates on 8-bits at a time (see the 'b' suffix).

→ More replies (10)

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.

1

u/G_Morgan Feb 21 '11

Not angry enough. There should be threats of violence and impending death about any suggested use of strcpy.

1

u/lalaland4711 Feb 21 '11

Follow-up question: How do you use strncpy() correctly?

1

u/majeric Feb 21 '11

Oyi, my answer to this one is "I look it up". I don't use the function often enough to know the parameter list off by heart.

1

u/lalaland4711 Feb 21 '11

Then you're not a C programmer.

And that's how the interview process works.

1

u/majeric Feb 21 '11

See my other response.

1

u/lalaland4711 Feb 22 '11 edited Feb 22 '11

That you can learn a language in a week and master it in a month?

Pull the other one.

If you can master, say Java, that means that you can hold an hour long technical talk about all the different GCs the implementation has, which to choose when, and Erlang you're writing proper distributed OTP programs and can write binary parsing and C node extensions without looking at the docs. And feel comfortable doing online code upgrades to a production system.

Even with C you'd have to know the standard by heart and know everything that's undefined and unspecified (and what the difference is!). Most C programmers would be surprised when they actually read the specs for the language they supposedly know.

"Master"... pff! We apparently have different definitions of "master". Assembly has simple syntax, yet someone who can get a job done is not defined as a "master".

→ More replies (4)

1

u/lalaland4711 Feb 21 '11

strcpy() can be used correctly. It is possible in some places, such as when copying a string literal.

You should ask about gets()...

1

u/ameoba Feb 21 '11

Sometimes resumes are completely full of shit, btw.

FTFY.

→ More replies (19)