Interviewing programmers: coding test example explained
61–70 of 178 posts
Re: Interviewing programmers: coding test example explained
#62I 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.
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'\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).
If (NULL == 0) isn't true you're not using C.
Re: Interviewing programmers: coding test example explained
#64Turns 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
#65Earlier 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…
Re: Interviewing programmers: coding test example explained
#66'\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-…
Yeah, it would be. They're NUL-terminated.
Re: Interviewing programmers: coding test example explained
#67Earlier 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.
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
#68I 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…
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
#69Earlier 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. :-)
Re: Interviewing programmers: coding test example explained
#70'\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).