Earlier quoted context omitted.
Why would he be alone? Why is spelling less important in code than in normal writing? I would argue it is considerably more important.
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…
Interviewing programmers: coding test example explained
51–60 of 178 posts
Re: Interviewing programmers: coding test example explained
#52I'm sure RiderOfGiraffes doesn't want a third entry from me at this point, so I figure I might as well just post it here: #define C char #define F for #define R condense_by_removing #define V void V R(C*A,C B){F(C*J=A;*J=*A++;J+=*J!=B);} or without #defines: void condense_by_removing(char*A,char B){for(char*J=A;*J=*A++;J+=*J!=B);}
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().
Re: Interviewing programmers: coding test example explained
#53I'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 clarity or ease in reasoning.
Re: Interviewing programmers: coding test example explained
#54 void condense_by_removing(
char *z_terminated ,
char char_to_remove
) {
char *rptr = z_terminated; // read ptr
char *wptr = z_terminated; // write ptr
for(;*rptr; rptr++) {
if (*rptr != char_to_remove) {
*wptr++ = *rptr;
}
}
*wptr = 0;
}Re: Interviewing programmers: coding test example explained
#55Am I OCD when I cringe when I see 'p_rite' where he (I think) means 'p_write'? That alone would knock a few points off a candidate doing this test when he's interviewing with me. 'rite' can mean 'write' or 'right' or even 'allright' - why the unnecessary confusion?
I think he used p_rite so that the variable names length is identical to p_read, which can make the code look more pretty.
Re: Interviewing programmers: coding test example explained
#56hmm, is it normal to assume all strings in C are \0 terminated? What are the memory usage implications for that? I won't pretend to know C but suppose you have a string that is 'ab{100}\0' and you wanted to remove all of the bs, you'd end up with 'a\0b{99}\0' in memory correct?
Re: Interviewing programmers: coding test example explained
#57Earlier quoted context omitted.
"In place" usually means that the result is in the same place as the input, and you don't create an intermediate copy. Creating a fix, known number of additional variables to assist with the calculation is OK. Some people allocated memory, copied out the input, then put back the bits they wanted to keep. That is not "in place".
Your routine should modify the given zero-terminated string in place, removing all instances of the given char. The main point of the whole exercise is to see if the candidate can write any code - anything after that is a bonus. Now if the input and output of a function are correct I think you where simply less clear than you may have thought. If you had said "string in place (don't allocate any memory), " I suspect…
As it says elsewhere, the purpose is to get some code, then use it as a start for the discussion. If someone allocates memory then that's where I start. In that case they clearly they don't understand the usual meaning of the expression "in-place."
> If you had said "string in place (don't allocate
> any memory)," I suspect far more people would have
> given you the output you wanted.
I suspect you're wrong, and I would be interested to see if anyone else comments on that point. I got nearly 100 submissions, and only one (from memory) allocated memory. All the others did the modification "in-place" as requested.Re: Interviewing programmers: coding test example explained
#58And 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 thi…
Re: Interviewing programmers: coding test example explained
#59I always end up using perl when I have to do string manipulations and have let my c skills suffer as a result. Sad. Using perl regexp kills brain cells. #!/usr/bin/perl $in = ; $remove = ; chomp ($remove); chomp ($in); $in =~ s/$remove//g;
>hello|goodbye
>|
>>Result is 'hello|goodbye'
>That's a nice dog you have there.
>.
>>Result is ''
I usually handle this with: my $pattern = '\\'.substr($in,0,1);
$in =~ s/$pattern//g;
but it still doesn't feel safe. String operations in perl usually do what you want, but be careful with them!Re: Interviewing programmers: coding test example explained
#60I 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…