Thread: Google Written Test question...!

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Google Written Test question...!

    I saw this on a facebook group.
    Many members quickly confirmed that this is indeed from a Google Written test they had appeared for.
    If they said it was from an interview, it would be easy to say that this was a trick question.
    But in a written test without extra options, what are people supossed to do when there is a known undefined behaviour/segfault/etc?

    I've heard stories about non-tech people conducting interviews which are obviously technical in nature...but this takes it a step further.

    And after seeing Google being praised as a developers' heaven in the media for a few years..this seems strange for me.

    Code:
    void f(char *x) 
    {
     
         x++;
     
         *x = ‘a’;
     
    }
     
    int main() 
    {
     
         char *str = "hello";
     
         f(str);
     
         printf("%s", str);
     
    }
     
     a) hello
     
     b) allo
     
     c) hallo
     
     d) empty string

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    The answer is c, right? Or is it an empty string?

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by MutantJohn View Post
    The answer is c, right? Or is it an empty string?

    You totally missed the point.
    There is no answer because you can not modify a string literal.
    It may or may not reside in the read only section of the executable.

    Question 1.32

    For example, it gives me a segfault.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by MutantJohn View Post
    The answer is c, right? Or is it an empty string?
    I hope that's a joke!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by manasij7479 View Post
    I saw this on a facebook group.
    Many members quickly confirmed that this is indeed from a Google Written test they had appeared for.
    It seems unlikely to me that "many members" would know that. More likely they heard the rumor before.

    Although it's obviously undefined behavior, it's not necessarily a segfault as it could work on some systems.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    But in a written test without extra options, what are people supossed to do when there is a known undefined behaviour/segfault/etc?
    Inform the relevant people that the test question is wrong. After all, tests can have bugs too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Right...only one explicitly said that. So, many was an exaggeration.

    And by relevant people, you mean the invigilators ?

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Would the answer have been c if the question was written as,
    Code:
    char a[6] = "hello";
    
    char *b = a;
    That way you actually have memory, I do believe, to modify. Or am I completely wrong? Gah, I'm so bad at tests.

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Alright, I just ran this code :

    Code:
    #include <stdio.h>
    
    
    void f(char *x) 
    {
      
         x++;
      
         *x = 'a';
      
    }
      
    int main(void) 
    {
      
         char test[6] = "hello";
    
    
         char *str = test;
      
         f(str);
      
         printf("%s\n", str);
      
    
    
         return 0;
    }
    and the output is :hallo

    Therefore, the answer is C!!!!!

    Ha ha ha! I can totally work for Google now. Even if that question was on a test, don't assume it's valid code. Just pretend and if you had to do the pointer arithmetic, would you get it right?

  10. #10
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Besides modifying a literal, notice that main is not given an explicit return value.
    I don't think this is Google's mistake though. If this indeed from Google byte-for-byte, they probably introduced these "errors" just so they could give points to any interviewees that would catch them, considering a sufficiently liberal compiler would make that program perfectly runnable.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Yarin View Post
    Besides modifying a literal, notice that main is not given an explicit return value.
    not sure about C, but in C++, not returning anything from main is defined to be the same as returning 0.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Yarin View Post
    Besides modifying a literal, notice that main is not given an explicit return value.
    Although horrible, C99 (and I think C89) do not require an explicit return in main

    5.1.2.2.3 Program termination
    1 If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;10) reaching the } that terminates the
    main function returns a value of 0
    . If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified.
    (bold is mine)

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by laserlight View Post
    Inform the relevant people that the test question is wrong. After all, tests can have bugs too.
    Or can be purposely misleading, no?

    I wish I could remember all the details of the last question on a 3 question exam in preparation for an interview for a software development company in Ireland. It included a quite misleading question. The code was shown as a test case and we were supposed to implement the function(s) which were meant to perform a recursive sum of the elements in the input array. The problem was unsolvable and points were awarded for pointing out all the flaws, which included:

    1) The test case wasn't atomic.
    2) The recursive function was expecting a single element as input.
    3) The helper function return value violated the non-destructive requirement for the input array.
    4) and a couple more...

    My only beef with it was that pointing out 1) was less awarded than the others.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by SirPrattlepod View Post
    Although horrible, C99 (and I think C89) do not require an explicit return in main
    I don't do the same reading of 5.1.2.2.3

    What I read in there is that you can omit the return statement. Not the return type. Void is not compatible with int.

    EDIT: Nevermind. I both misread your answer and who you were answering to.
    Last edited by Mario F.; 10-25-2013 at 05:48 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Mario F. View Post
    I don't do the same reading of 5.1.2.2.3

    What I read in there is that you can omit the return statement. Not the return type. Void is not compatible with int.
    That's what I said...? The original code is int main(void) and omits the return statement, which is fine so far as complying with the standard (we're talking about the return value)

    Edit: perhaps my wording was rushed and contained assumptions based on previous comments; but, yes, I was indeed referring to the return statement (and the value returned if it's omitted) and not the return type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General question why must of the servers written in c and not c++ ?
    By umen242 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-26-2008, 04:17 AM
  2. Reading Google Sketchup (.skp) or Google Earth (.kmz)
    By Zeeshan in forum Game Programming
    Replies: 9
    Last Post: 03-07-2008, 05:25 PM
  3. Question of test c++
    By endrzer in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 10:07 AM
  4. test question
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 06-23-2005, 07:59 PM