|
-
February 27th, 2006, 03:10 PM
#1
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 ?
-
February 27th, 2006, 03:19 PM
#2
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
-
February 27th, 2006, 03:34 PM
#3
Re: Ranking from fast to slow .. interview question
-
February 27th, 2006, 03:54 PM
#4
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.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
February 27th, 2006, 04:11 PM
#5
Re: Ranking from fast to slow .. interview question
its a preselection test. Got tons of technical questions i had to answer on paper
-
February 27th, 2006, 04:59 PM
#6
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.
Kevin Hall
-
February 27th, 2006, 05:14 PM
#7
Re: Ranking from fast to slow .. interview question
 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.
-
February 27th, 2006, 05:17 PM
#8
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 27th, 2006, 07:18 PM
#9
Re: Ranking from fast to slow .. interview question
 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.
Last edited by RoboTact; February 27th, 2006 at 07:21 PM.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
February 27th, 2006, 07:19 PM
#10
Re: Ranking from fast to slow .. interview question
 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 ?
-
February 27th, 2006, 07:25 PM
#11
Re: Ranking from fast to slow .. interview question
 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.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
February 27th, 2006, 07:32 PM
#12
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.
-
February 28th, 2006, 01:33 AM
#13
Re: Ranking from fast to slow .. interview question
 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... I was going to reply with that until I saw your mention of it. Best regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 28th, 2006, 02:37 AM
#14
Re: Ranking from fast to slow .. interview question
 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
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 28th, 2006, 02:45 AM
#15
Re: Ranking from fast to slow .. interview question
 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
C++ program ran... C++ program crashed... C++ programmer quit !!   
Regards
Shaq
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|