Ranking from fast to slow .. interview question
I just had a technical interview ...
I had to rank from fastest to slowest ...
1)
Code:
for( int i =0;i<16;i++)
array1[i] = array2[i];
2)
Code:
for(int i=0;i<16;i++)
(int)array1[i] = (int)array2[i];
3)
Code:
memmove(dest,src,16);
4)
Code:
for(int i=0;i<16;i++)
for(int j=0;j<16;j++)
array1[i]=array2[j];
My answer : 3,1,2,4
Was it right ?
Re: Ranking from fast to slow .. interview question
The correct answer is
5) None of the above.
It depends on the compiler's optimizer. That was a trick question.
Regards,
Paul McKenzie
Re: Ranking from fast to slow .. interview question
Re: Ranking from fast to slow .. interview question
Well, this doesn't sound like an interview question. Unless you are interviewed by someone from this thread ...
BTW, you got #4 right - it does 16 times more work than #1..3. Of course, it's just writing different values over and over to the same location. Can't imagine why the speed would be an issue there.
Re: Ranking from fast to slow .. interview question
its a preselection test. Got tons of technical questions i had to answer on paper
Re: Ranking from fast to slow .. interview question
Another (and probably more important question) is "What is correct"?
The call to memmove() would only be correct if both arrays had elements one byte in size.
- Kevin
BTW, (2) might effectively be the same statement as (1) if the arrays were arrays of integers.
Re: Ranking from fast to slow .. interview question
Quote:
Originally Posted by Paul McKenzie
It depends on the compiler's optimizer. That was a trick question.
I have this feeling that the person that asked the question did not think at that. ;)
Re: Ranking from fast to slow .. interview question
I think I'd deliberately fail that interview. A company that asks questions like that probably isn't worth working for.
Re: Ranking from fast to slow .. interview question
Quote:
Originally Posted by cilu
I have this feeling that the person that asked the question did not think at that. ;)
I once participated in 'competition' put by sponsor (hardware trade company) during lunch break on programming contest... They wrote some mix of C++ and Java code (mostly resembling C++) and asked tricky question about behaviour of objects with multiple inheritance and type casting. I wrote 'compile error' as answer and they ranked it as (the only) correct answer. Only two people there knew that sponsor good enough to answer in such manner. :D
Re: Ranking from fast to slow .. interview question
Quote:
Originally Posted by Graham
I think I'd deliberately fail that interview. A company that asks questions like that probably isn't worth working for.
What do you mean ?
Re: Ranking from fast to slow .. interview question
Quote:
Originally Posted by GoDaddy
What do you mean ?
If they asked that question with intention to actually get some 'right' order, that question is just incompetence made manifest, which doesn't speek good for the firm.
Re: Ranking from fast to slow .. interview question
In 2), the left hand side is not even an lvalue so it produces a compilation error.
Re: Ranking from fast to slow .. interview question
Quote:
Originally Posted by googler
In 2), the left hand side is not even an lvalue so it produces a compilation error.
You stole that from me... :thumb: I was going to reply with that until I saw your mention of it. Best regards.
Re: Ranking from fast to slow .. interview question
Quote:
Originally Posted by googler
In 2), the left hand side is not even an lvalue so it produces a compilation error.
I think that point in 2) is that operating on ints is faster than on non-int numbers due to fact that int size should be natural data size for given architecture. Anyway, this question is not good one, I think...
Regards
Re: Ranking from fast to slow .. interview question
Quote:
Originally Posted by Hobson
I think that point in 2) is that operating on ints is faster than on non-int numbers due to fact that int size should be natural data size for given architecture
int size need not be the natural data size for given architecture