3 ms·
My first programming job interview (like 100 years ago) was a group interview. One guy asked me to write string compare in C. Not sure I did it right, but I b
by timmg 5y ago
My first programming job interview (like 100 years ago) was a group interview. One guy asked me to write string compare in C.
Not sure I did it right, but I basically walked the two char* pointers looking for a '0' or a mismatch. The guy asking the question said, "No, first you should compare the string lengths -- since if they have a different length they will be different."
I was nervous, but I thought he was wrong. I said, "Well, to get the length you need to walk both strings, so that isn't faster."
He got annoyed and said, "They have optimized functions for that!"
I didn't argue. Needless to say, I didn't get the job :)
- dekhn 5y agoModern strcmp and strlen both contain optimizations based on modern hardwarae, and I believe strlen can be faster than strcmp, but that's overoptimizing for performance.