Live data from Hacker News

Interviewing programmers: coding test example explained

solipsys.co.uk

61–70 of 178 posts

Re: Interviewing programmers: coding test example explained

#61
Turns out his solution is almost the same as mine except I skip the termination stage and instead read one character more in the loop which catches the \0 from the original end of the string automatically. As I'm not really a C programmer, was I doing something bad/unrecommended? It seemed to work.. :-)

Re: Interviewing programmers: coding test example explained

#62

I wonder if I'm alone in thinking that the original `while` version is easier to read and understand than the ending `for` version. I've never quite understood C programmers' love of the `for` loop. It's just a `while` loop with the different parts stuck in different places (`init; while (cond) { ...; inc; }` is the same as `for(init; cond; inc) { ...; }`) and it doesn't (at least for me) result in any greater clarit…

Actually, they are not the same. See the trivia question in the lunk article.

I saw the trivia question in the link article, but it doesn't provide an answer, so I ignored it.

EDIT: K&R says (page 60 in my second edition):

""" The for statement

  for (expr1; expr2; expr3)
    statement
is equivalent to

  expr1;
  while (expr2) {
    statement
    expr3;
  }
except for the behavior of continue, which is described in Section 3.7. """

So apparently the trivia is the behavior of `continue`, which I think most C programmers (including myself) would know implicitly, even if we couldn't answer the question itself.

Re: Interviewing programmers: coding test example explained

#63
post #27

'\0' is null-terminated NOT zero-terminated, no ? By definition, a string in C is always null-terminated.

'\0' is a character, not a string. The string is "\0". Also, C strings are NUL -terminated, not NULL -terminated. (char)(0) is the NUL character; (void * )(0) is the NULL pointer (usually).

Actually it's the NULL pointer by definition. 0 is always equivalent to NULL in pointer context, even though you're working with some hypothetical crazy system where NULL is actually #define NULL 1337. It's mandated by the standard.

If (NULL == 0) isn't true you're not using C.

Re: Interviewing programmers: coding test example explained

#64

Turns out his solution is almost the same as mine except I skip the termination stage and instead read one character more in the loop which catches the \0 from the original end of the string automatically. As I'm not really a C programmer, was I doing something bad/unrecommended? It seemed to work.. :-)

Do you want to email me your reference number and I'll have a chat about it. It would be useful for me to clarify the next article on this point.

Re: Interviewing programmers: coding test example explained

#65
post #36

Earlier quoted context omitted.

If you truly think the market for C developers is so favorable to employers that someone who can write the in-place-whitespace-removal function off the top of their heads is easy to find, then by all means, ding candidates for their spelling during interviews. Thanks for making it that much easier for the rest of us to hire. In the industry I work in --- computer software --- it is ludicrously hard to find people com…

I personally would count that against any candidate during an interview. I wouldn't necessarily reject them, but I would definitely count that against them. I work in Toronto, Ontario. English is not my native language, but I always do my best to ensure that my code or documentation is as close to crystal clear as possible. This starts with using correct spelling. Code is written first for humans to read, and to me m…

But if you're talking about a whiteboard coding problem, people are going to come in with different perceptions of what you're trying to test, and for the sake of brevity they'll do things like use shorter names or global variables. I feel like I'd cringe at seeing "rite," but not as much as when somebody thinks it looks impressive to start by showing you the testing code they would write.

Re: Interviewing programmers: coding test example explained

#66
post #40
post #27

'\0' is null-terminated NOT zero-terminated, no ? By definition, a string in C is always null-terminated.

As it happens, '\0' translates to 0. Since they are composed of characters, C programmers tend to do the c == '\0' check instead of the shorted c == 0 check, just to make sure everyone knows we are still treating the char like a character in a string; it is just style. char c = 65 is equivalent to and just as legal as char c = 'A' So yes, C strings ARE zero-terminated. In fact, it is a bit weird to say they are null-…

>it is a bit weird to say they are null-terminated

Yeah, it would be. They're NUL-terminated.

Re: Interviewing programmers: coding test example explained

#67
post #40

Earlier quoted context omitted.

As it happens, '\0' translates to 0. Since they are composed of characters, C programmers tend to do the c == '\0' check instead of the shorted c == 0 check, just to make sure everyone knows we are still treating the char like a character in a string; it is just style. char c = 65 is equivalent to and just as legal as char c = 'A' So yes, C strings ARE zero-terminated. In fact, it is a bit weird to say they are null-…

>it is a bit weird to say they are null-terminated Yeah, it would be. They're NUL-terminated.

Unfortunately, "null-terminated", although incorrect, is widely used. :(

http://googlefight.com/index.php?lang=en_GB&word1=%22nul...

I hate all the variations of nothing. undef, null, NULL, NIL, NUL...

Re: Interviewing programmers: coding test example explained

#68

I wonder if I'm alone in thinking that the original `while` version is easier to read and understand than the ending `for` version. I've never quite understood C programmers' love of the `for` loop. It's just a `while` loop with the different parts stuck in different places (`init; while (cond) { ...; inc; }` is the same as `for(init; cond; inc) { ...; }`) and it doesn't (at least for me) result in any greater clarit…

I think for loops are better than while loops for looping over arrays. I'll try and explain my reasoning:

Whenever you see a `for` loop, it tells you something that a `while` loop doesn't. It tells you the kind of loop you're about to do.

A `for` (usually) means you're going to be looping over an array, with a specific length, with a specific "step" (usually one). This is information you immediately get by seeing a `for`.

Moreover, you get all the information about how the loop looks (what you're looping over, size of the array, other actions you intend to perform in the loop) all in one place. This especially helps when you're looking at a large piece of code, in which case your example of "while (cond) {...;inc;}" would have a lot of lines between the condition and the increment.

Re: Interviewing programmers: coding test example explained

#69

Earlier quoted context omitted.

Here is the same code, without syntactic obfuscation: void condense_by_removing(char* s, char c) { char* d = s; while (*d = *s++) d += *d != c; } I would have sworn I found a bug. There is none. Brilliant. Now, I wonder if we could further optimize it. For instance by accessing memory several bytes at a time, in a fashion similar to strcmp().

Optimizing for performance, sure. But I was trying to optimize for character count. :-)

Yes, but in doing so, you traded a memory write for a branch. And memory accesses are linear, so that's likely faster than the cannon. So surely we could go further? That's how I got the idea.

Re: Interviewing programmers: coding test example explained

#70
post #27

'\0' is null-terminated NOT zero-terminated, no ? By definition, a string in C is always null-terminated.

'\0' is a character, not a string. The string is "\0". Also, C strings are NUL -terminated, not NULL -terminated. (char)(0) is the NUL character; (void * )(0) is the NULL pointer (usually).

The C standard exclusively uses the phrase "null-terminated" to refer to strings.
Post reply on HN