CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 43
  1. #1
    Join Date
    Feb 2006
    Posts
    69

    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 ?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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

  3. #3
    Join Date
    Feb 2006
    Posts
    69

    Re: Ranking from fast to slow .. interview question

    :O crap

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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...

  5. #5
    Join Date
    Feb 2006
    Posts
    69

    Re: Ranking from fast to slow .. interview question

    its a preselection test. Got tons of technical questions i had to answer on paper

  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    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

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  9. #9
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    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.
    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."

  10. #10
    Join Date
    Feb 2006
    Posts
    69

    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 ?

  11. #11
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    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.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  12. #12
    Join Date
    Dec 2005
    Posts
    642

    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.

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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... I was going to reply with that until I saw your mention of it. Best regards.

  14. #14
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  15. #15
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    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
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured