News for hackers. Stuff that matters.
Re:Interviewing programmers: coding test example explained by loup-vaillant on Thursday June 03, 2010 @09:56AM (#1401293)
And I thought my solution was as simple as possible. If only I decoupled my two increments, I may have avoided this unreadable, inefficient crap:

  void remove_char(char *s ,char c)
  {
    int from = -1;
    int to   = -1;
    do {
      from++; to++;
      while (s[from] == c) from ++;
      s[to] = s[from];
    } while (s[from] != '\0');
  }
Maybe that's why simplicity doesn't actually rule: it's hard to find. Or, people are silly (including myself in this case).
Re:Interviewing programmers: coding test example explained by jemfinch on Thursday June 03, 2010 @11:16AM (#1401527)
Your code also fails (i.e., reads and writes past the end of the string) if c == '\0' and s is "\0".
Re:Interviewing programmers: coding test example explained by loup-vaillant on Monday June 07, 2010 @03:02AM (#1410049)
I considered the `c=='\0'` case bogus anyway, so I was OK with a partial function. (I wouldn't have been for production code, though.)
Re:Interviewing programmers: coding test example explained by RiderOfGiraffes on Monday June 07, 2010 @03:18AM (#1410066)
Are you saying that you actually considered that case, and then wrote code that didn't deal with it?
Re:Interviewing programmers: coding test example explained by loup-vaillant on Tuesday June 08, 2010 @09:23AM (#1413806)
Yes I did, and then yes I did.

I didn't fully investigated that case, but I suspected there could be a problem with `c=='\0'`. I suspected that the `while (s[from] == c) from ++;` line could shoot past the termination '\0' and trigger a buffer overflow.

But I didn't fully investigate, on the grounds that no sane programmer would want to remove a character that's never in a C string. In other words, I considered it was not part of the specification.

In production code, I would have either thrown an exception, or returned early. And I would have asked around to know which I would chose.

Could my attitude have influenced my chances, if I had applied?