Interview pro-tip



  • If you are given a five line function and asked "What's wrong with this function?"

    DON'T critique the coding style (eg input parameter should be fileName, not filename) 

    DO point out the freaking infinite loop.



  • True enough.

    Were you the interviewer or interviewee? Context?



  • Interviewer.

    Have had several interviewees do pretty much this same thing.   I could understand it if the question said "critique the following code."  But when the question is "What's WRONG?", it's pretty obvious that you should be looking for a bug. Of course, based on their replies to the other questions on the sheet, I guess I shouldn't be surprised that they missed the bug



  • Just for fun, are you able to share the code you used?


  • Discourse touched me in a no-no place

    @Xyro said:

    Just for fun, are you able to share the code you used?
    +1


  • Trolleybus Mechanic

    @PJH said:

    @Xyro said:
    Just for fun, are you able to share the code you used?
    +1
     

    Should be 1+.  Prefixing the incriment is for COBOL greybeards!



  • @campkev said:

    But when the question is "What's WRONG?", it's pretty obvious that you should be looking for a bug.
     

    Technically, you're right - but it comes down to your definition/interpretation of wrongness:

    • poorly-structured unmaintainable code containing no bugs smacks of wrongness for me
    • code that fulfills incorrect specs is still correct, but wrong in the eyes of the customer

    Maybe if the question was phrased as "find the bugs in this code, if any" and several code snippets were presented, some containing several bugs and some containing none, would help to highlight the developer's defect-detection ability.

    The same code snippets could be repurposed with a further question of "if you had the chance to change any of the code presented, what would you change - and why?" opens discussions about good/bad coding styles, reasons for specific structures, understanding of maintainability/commenting/layout etc:

    • I expect a good dev to relate the better-structured/more maintainable snippets to quicker/easier defect identification, thus understanding how standards reduce debugging times and effort.
    • It also allows pompous twunts the opportunity to sneer as much as they liked and expose their sense of self-importance and one-upmanship quickly. And flag up the "hell, noes!" for those that think the SpectateSpagettiRandomMess snippet is acceptable quality.
    • you can identify those that think gold-plating software is important
    etc.


  • @Lorne Kates said:

    @PJH said:

    @Xyro said:
    Just for fun, are you able to share the code you used?
    +1
     

    Should be 1+.  Prefixing the incriment is for COBOL greybeards!

    Don't you mean COBOL whitebeards? 

     



  • @campkev said:

    If you are given a five line function and asked "What's wrong with this function?"

    DON'T critique the coding style (eg input parameter should be fileName, not filename) 

    DO point out the freaking infinite loop.

    Here is a partial explanation for this behavior. In the context of hiring a programmer, the importance of the interview is usually higher for the candidate than the interviewer (simple economics: one job, multiple candidates) and often the candidate will be eager to answer implicit questions and make it clear that he knows his stuff. Pointing out coding style issues or challenging questions is a symptom of that eagerness, it does not mean the candidate is a moron.

    Asking what is "wrong" with a piece of code is an open question; it is an excellent choice for a technical interview unless you expect a single answer like it's some kind of high school test. An interview is an opportunity for both parties to see if the candidate is a good match for the position, and it is a dialog; so when you receive an answer that is different from what you expected, you take a note because the information provided can add some depth to the profile, and then you specify that you are looking for a logic flaw or a possible bug. If you are not interested in that dynamic, just give them a multiple choice questionnaire and go grab a coffee or something because otherwise you will give a poor image of the company.

    So I disagree with your suggestion. People should critique the coding style AND point out the infinite loop, but it's up to the interviewer to make sure that the candidate is comfortable to do so.



  • @airdrik said:

    Don't you mean COBOL whitebeards?
    Seriously? You wanted to kick up "greybeards" a notch and the best you could come up with was changing "grey" to "white"?

    (shakes head sadly)

     


  • Trolleybus Mechanic

    @Zylon said:

    @airdrik said:

    Don't you mean COBOL whitebeards?
    Seriously? You wanted to kick up "greybeards" a notch and the best you could come up with was changing "grey" to "white"?

    (shakes head sadly)

     

     

    What about camelBeards?

     



  • @Zylon said:

    @airdrik said:

    Don't you mean COBOL whitebeards?
    Seriously? You wanted to kick up "greybeards" a notch and the best you could come up with was changing "grey" to "white"?

    Worked for Gandalf.

     



  • @Cassidy said:

    @campkev said:

    But when the question is "What's WRONG?", it's pretty obvious that you should be looking for a bug.
     

    Technically, you're right - but it comes down to your definition/interpretation of wrongness:

     

     

    It may change with the definition of wrongness, but I think a freaking infinite loop would fit any definition you can come up with.

     

    I would accept people to point other problems, as you said, the question is quite open. But not pointing the infinite loop? No way.

     



  • @Mcoder said:

    It may change with the definition of wrongness, but I think a freaking infinite loop would fit any definition you can come up with. I would accept people to point other problems, as you said, the question is quite open. But not pointing the infinite loop? No way.

    I tend to agree. I'm not really sure it's BIQE (best interview question ever), but not seeing an infinite loop in five lines does not give a great impression.

    I had an interview once where they asked me to write down a routine to compute the n-th Fibonacci number. I was confused. What kind of trick was this? So, I wrote down a simple loop, including the loop invariant to make sure I got the n-th one, and not the one before or after. Answered a few questions about the solution, everybody happy. To bad I didn't remember the closed variant, but hey.

    Turns out that quite a few of the interviewed programmers couldn't do something so trivial, or wrote the recursive solution without being able to defend it or change the solution when faced with the obvious criticism.

    So yeah, when someone asks you in an interview what's wrong, the infinite loop should have a pretty high priority.



  • @Mcoder said:

    @Cassidy said:

    @campkev said:

    But when the question is "What's WRONG?", it's pretty obvious that you should be looking for a bug.
     

    Technically, you're right - but it comes down to your definition/interpretation of wrongness:

     

     

    It may change with the definition of wrongness, but I think a freaking infinite loop would fit any definition you can come up with.

     

    I would accept people to point other problems, as you said, the question is quite open. But not pointing the infinite loop? No way.

     

    This. I would have disagreed had you not qualified your argument by saying that none of the candidates actually ever pointed out the defect. It would be one thing if they pointed out both the stylistic foible and the bug, but missing an obvious bug in 4 friggin' lines of code is just wrong.



  • not the exact code but close enough to give the idea:

     

    public void OutputText(string filename)

    {

     using (StreamReader sr = new StreamReader(filename))

        {

             string temp = sr.ReadLine();

             while( temp != null)

             {

                  Console.WriteLine(temp);

             }

        }

    }



  • @campkev said:

    not the exact code but close enough to give the idea:

     

    public void OutputText(string filename)

    {

     using (StreamReader sr = new StreamReader(filename))

        {

             string temp = sr.ReadLine();

             while( temp != null)

             {

                  Console.WriteLine(temp);

             }

        }

    }

    I'd change the parameter name to fileName...


  • @Mcoder said:

    But not pointing the infinite loop? No way.
     

    Aha, okay... I didn't read it as "missing the infinite loop", I read it as "my coding style is NOT wrong".

    But yup - missing this show-stopper is.. an interview-stopper, really.



  • @campkev said:

    not the exact code but close enough to give the idea:

     

    public void OutputText(string filename)

    {

     using (StreamReader sr = new StreamReader(filename))

        {

             string temp = sr.ReadLine();

             while( temp != null)

             {

                  Console.WriteLine(temp);

             }

        }

    }

    The error is to use Console instead of a custom library to output data.



  •  I'm not seeing the infinite loop.

     " The next line from the input stream, or null if the end of the input stream is reached." - MSDN, StreamReader.Readline();

     

    And after I typed that, I saw it, Staring me right in the face.  Such a noob mistake that the brain just sort of blocks out it's existence.



  • @Speakerphone Dude said:

    @campkev said:

    not the exact code but close enough to give the idea:

     

    public void OutputText(string filename)

    {

     using (StreamReader sr = new StreamReader(filename))

        {

             string temp = sr.ReadLine();

             while( temp != null)

             {

                  Console.WriteLine(temp);

             }

        }

    }

    The error is to use Console instead of a custom library to output data.



    No, the real error is not using "cat filename" to accomplish this.

     

     



  • @TGV said:

    I had an interview once where they asked me to write down a routine to compute the n-th Fibonacci number. I was confused. What kind of trick was this? So, I wrote down a simple loop, including the loop invariant to make sure I got the n-th one, and not the one before or after. Answered a few questions about the solution, everybody happy. To bad I didn't remember the closed variant, but hey.

    Turns out that quite a few of the interviewed programmers couldn't do something so trivial, or wrote the recursive solution without being able to defend it or change the solution when faced with the obvious criticism.

     

    phi = (1 + sqrt(5))/2

    Fib = (phi^n - (-1)^n/phi^n) / sqrt(5)

    Loops are for engineers.

    As for critiquing the coding style when they ask you what's wrong with a sample function, I'm reminded of this exchange from June of 1962:

    George Martin:  "Tell me if there's anything you don't like."

    George Harrison:  "Well, for a start, I don't like your tie."

     



  • looks like C#, I hate curly braces.

    @campkev said:

    not the exact code but close enough to give the idea:

    public void OutputText(string filename)
    {
     using (StreamReader sr = new StreamReader(filename))
        {
             string temp = sr.ReadLine();
             while( temp != null)
             {
                  Console.WriteLine(temp);
             }
        }
    }

    Public Sub OutputText(filename As String)
         Using sr As New StreamReader(filename)
              Dim temp As String = sr.ReadLine()
                   While temp IsNot Nothing
                       Console.WriteLine(temp)
                       temp  = sr.ReadLine()
                    End While
          End Using
    End Sub



  • @mightybaldking said:

    @Speakerphone Dude said:

    @campkev said:

    not the exact code but close enough to give the idea:

     

    public void OutputText(string filename)

    {

     using (StreamReader sr = new StreamReader(filename))

        {

             string temp = sr.ReadLine();

             while( temp != null)

             {

                  Console.WriteLine(temp);

             }

        }

    }

    The error is to use Console instead of a custom library to output data.



    No, the real error is not using "cat filename" to accomplish this.

     

     

    Because of the lower case in "string" I was thinking this is C#, in which case cat filename is a bad answer, it should be type filename.



  • @campkev said:

    The error is to use Console instead of a custom library to output data.

     

    But sometimes all you have is a console.  Such as in embedded devices.

     



  • @da Doctah said:

    phi = (1 + sqrt(5))/2

    Fib = (phi^n - (-1)^n/phi^n) / sqrt(5)

    Loops are for engineers.


    What would you do if I told you that there's a BigInteger class available and I want exact results for any n up to 200?



  • @Speakerphone Dude said:

    Because of the lower case in "string" I was thinking this is C#, in which case cat filename is a bad answer, it should be type filename.
    Because C# absolutely only runs on Windows. And there is no way you can ever run cat in Windows.



  • @Zecc said:

    @Speakerphone Dude said:

    Because of the lower case in "string" I was thinking this is C#, in which case cat filename is a bad answer, it should be type filename.
    Because C# absolutely only runs on Windows. And there is no way you can ever run cat in Windows.

    While your two statements are lame on their own, when you put them together it makes them look even worse. It's one of those rare cases when the whole is smaller than the sum of all parts. Bravo.


  • BINNED

    @Zecc said:

    @Speakerphone Dude said:

    Because of the lower case in "string" I was thinking this is C#, in which case cat filename is a bad answer, it should be type filename.
    Because C# absolutely only runs on Windows. And there is no way you can ever run cat in Windows.

     

    So you're saying you'd install a (cygwin or whatever) cat.exe and use a system() call instead of those few lines of code? And that's an improvement???

     



  • @Speakerphone Dude said:

    @Zecc said:
    @Speakerphone Dude said:
    I think we should run an executable instead of writing a function.
    I think we should run a different executable.
    I am angry at you because you argued against my point.
    Summarized that for you



  • @Ben L. said:

    @Speakerphone Dude said:
    @Zecc said:
    @Speakerphone Dude said:
    I think we should run an executable instead of writing a function.
    I think we should run a different executable.
    I am angry at you because you argued against my point.
    Summarized that for you

    Your summaries are so fair and balanced, you should send your resume to Fox News.



  • @Speakerphone Dude said:

    @Zecc said:

    @Speakerphone Dude said:

    Because of the lower case in "string" I was thinking this is C#, in which case cat filename is a bad answer, it should be type filename.
    Because C# absolutely only runs on Windows. And there is no way you can ever run cat in Windows.

    While your two statements are lame on their own, when you put them together it makes them look even worse. It's one of those rare cases when the whole is smaller than the sum of all parts. Bravo.

    Don''t forget to count your own statement!


  • @topspin said:

    @Zecc said:

    @Speakerphone Dude said:

    Because of the lower case in "string" I was thinking this is C#, in which case cat filename is a bad answer, it should be type filename.
    Because C# absolutely only runs on Windows. And there is no way you can ever run cat in Windows.

     

    So you're saying you'd install a (cygwin or whatever) cat.exe and use a system() call instead of those few lines of code? And that's an improvement???

    No, I was just nitpicking and pointing out that C# doesn't necessarily imply using type.

    But you could interpret my post as a continuation of the chain of growing silliness:  (using Console.WriteLine)  < (running external type process) << (installing cygwin to run cat on Windows) << (switching over to Mono and Linux for the sole purpose of running cat)

     



  • @pjt33 said:

    @da Doctah said:

    phi = (1 + sqrt(5))/2

    Fib = (phi^n - (-1)^n/phi^n) / sqrt(5)

    Loops are for engineers.

    What would you do if I told you that there's a BigInteger class available and I want exact results for any n up to 200?
     

    Ask you why you want all these big honkin' Fibonacci numbers?

    At which point I'd suggest that if you have some application that regularly requires the first 200 Fibonacci numbers, you should probably calculate them all in advance and stick them in a table.

     



  • @da Doctah said:

    Ask you why you want all these big honkin' Fibonacci numbers?


    Indeed. With I my interviewer hat on, I was probing to see whether you recognise the weakness of using Binet's formula for calculations (as opposed to proving identities).

    @da Doctah said:

    At which point I'd suggest that if you have some application that regularly requires the first 200 Fibonacci numbers, you should probably calculate them all in advance and stick them in a table.


    And we come full circle with "And how would I calculate them to put them in the table?"



  • @pjt33 said:

    @da Doctah said:

    Ask you why you want all these big honkin' Fibonacci numbers?

    Indeed. With I my interviewer hat on, I was probing to see whether you recognise the weakness of using Binet's formula for calculations (as opposed to proving identities).

    @da Doctah said:

    At which point I'd suggest that if you have some application that regularly requires the first 200 Fibonacci numbers, you should probably calculate them all in advance and stick them in a table.


    And we come full circle with "And how would I calculate them to put them in the table?"
     

    I'd love to see a design that requires that the first 200 Fibonacci numbers be put in a table. It must be one of the Cardinal WTFs.

    Anyway, who cares how you put them in the table, as long as it's filled, and doesn't takes ages.



  • @mightybaldking said:

     I'm not seeing the infinite loop.

    And after I typed that, I saw it, Staring me right in the face.  Such a noob mistake that the brain just sort of blocks out it's existence.

     

    I... saw it now, too.

    Which lends credence to my hypothesis that your brain just randomly comes up with ideas and sometimes one of them is the right answer and where intelligence is just the efficiency of your random number generator.

     


  • BINNED

    @dhromed said:

    I... saw it now, too.

    Which lends credence to my hypothesis that your brain just randomly comes up with ideas and sometimes one of them is the right answer and where intelligence is just the efficiency of your random number generator.


    +1

    At first I thought the problem was that maybe the function returns an empty string instead of null, so the loop would never break. And thought that was quite non-obvious to catch.

    Then I was like "man, I'm stupid" when I saw the call is outside of the loop.

     



  • @dhromed said:

    Which lends credence to my hypothesis that your brain just randomly comes up with ideas and sometimes one of them is the right answer and where intelligence is just the efficiency of your random number generator.

    A programmer trying his hand at cognitive psychology. You think memory is not a factor in this? Or that it's the other way around: that you generate a lot of alternatives, and intelligence is the quality of the selection heuristic? Or that there is a feedback loop?



  • @TGV said:

    Or that it's the other way around: that you generate a lot of alternatives,
     

    There is some vague preselection occurring, of course. When thinking about a programming problem, you're not likely to think about the atomic weight of osmium, for instance.

    @TGV said:

    and intelligence is the quality of the selection heuristic?

    Not a clue.

    Let me just ponder the issue at random times and then a solution will spring to mind. 

     



  • @dhromed said:

    @TGV said:

    Or that it's the other way around: that you generate a lot of alternatives,
     

    There is some vague preselection occurring, of course. When thinking about a programming problem, you're not likely to think about the atomic weight of osmium, for instance.

    @TGV said:

    and intelligence is the quality of the selection heuristic?

    Not a clue.

    Let me just ponder the issue at random times and then a solution will spring to mind. 

     

    Liberals, all of yous. Says the study:

    @Science People said:

    People with self-reported liberal views tended to have a larger anterior cingulate cortex -- a brain area involved in processing conflicting information. And those with conservative views were more likely to have a larger amygdala -- a region important for recognizing threats.

    More info (speaker warning: video clip is on autoplay)



  •  This code is grossly underperformant. I would make

     

    <font face="terminal,monaco">
    </font>

    <font face="terminal,monaco">         while( true)</font>

    <font face="terminal,monaco">         {</font>

    <font face="terminal,monaco">              Console.WriteLine(sr.ReadLine());</font>

    <font face="terminal,monaco">              Console.WriteLine(sr.ReadLine()); // prevent excessive looping overhead
    </font>

    <font face="terminal,monaco">              Console.WriteLine(sr.ReadLine());</font>

    <font face="terminal,monaco">         } // exit on null  </font>



     



  • @campkev said:

    not the exact code but close enough to give the idea:

     

    public void OutputText(string filename)
    {
        using (StreamReader sr = new StreamReader(filename))
        {
            string temp = sr.ReadLine();
            while( temp != null)
            {
                Console.WriteLine(temp);
            }
        }
    }

    Right. So we were actually told that there's an infinite loop in there, and yet it took some of us (including myself) a little bit of time to actually see it. Which leads me to believe that the combination of the nicely formatted code, the using-construct which looks like a loop, and the somewhat ambiguous question, leads candidates to simply not see the bug. Remember, they are under stress, and the infinite loop is a pretty stupid thing to do. I'd never write code like that, so it didn't immediately occur to me that somebody else might.

    So before critisising all those dumb candidates, perhaps you should review your question, think of all aspects of it (after all, you know that you're writing an infinite loop, and that's the answer that you expect), and perhaps give it to your co-workers; after all, they did make it through the selection process.

    It would be interesting to find out the results.



  •  i have to agree. When possible, always do a dry run of your interview tests on your co worker it might:

    either prove your test is not reasonable

    or proove your co worker is grossly over paid :)




  •  @Severity One said:

    So we were actually told that there's an infinite loop in there, and yet it took some of us (including myself) a little bit of time to actually see it. Which leads me to believe that the combination of the nicely formatted code, the using-construct which looks like a loop, and the somewhat ambiguous question, leads candidates to simply not see the bug.

     

    Indeed. I never programmed in C# except for some "Hello, World" stuff and I actually went over to MSDN to figure out the peculiarities in StreamReader or maybe variable assignment in C# with regards to null, when it hit me in the head. It is such a simple coding mistake that you would notice in the first debug run before the loop is done with the second iteration that I didn't even consider this to be the bug... I think that yes, under stress I might not have noticed it either... fun bug for a forum though :D



  • @Speakerphone Dude said:

    @Science People said:

    People with self-reported liberal views tended to have a larger anterior cingulate cortex -- a brain area involved in processing conflicting information. And those with conservative views were more likely to have a larger amygdala -- a region important for recognizing threats.

    More info (speaker warning: video clip is on autoplay)

     

    But does "larger brain area" generally mean "more processing and correct outcomes" or "hyperactive"?

    Because it seems like the latter; since conservative views correlate with seeing threats everywhere (terrorists! aaah! gays! aaah!) and being generally fearful of the world at large (also somehow believing that this is not a tiresome and unproductive state of being).

    And does my apparently enlarged cingulate cortext mean I'm more accepting of bullshit, or less?

     

     

     probably more.

    :\


  • ♿ (Parody)

    @dhromed said:

    But does "larger brain area" generally mean "more processing and correct outcomes" or "hyperactive"?

    More likely, it's just phrenology dressed up with more modern measurements and small p-values.



  • @boomzilla said:

    @dhromed said:
    But does "larger brain area" generally mean "more processing and correct outcomes" or "hyperactive"?

    More likely, it's just phrenology dressed up with more modern measurements and small p-values.

     

    You seem threatened by this conflictinginformation which I can easily parse.

     

    God, I need a drink.


  • ♿ (Parody)

    @dhromed said:

    @boomzilla said:
    @dhromed said:
    But does "larger brain area" generally mean "more processing and correct outcomes" or "hyperactive"?

    More likely, it's just phrenology dressed up with more modern measurements and small p-values.

    You seem threatened by this conflictinginformation which I can easily parse.

    God, I need a drink.

    I could believe that one of those statements is accurate.



  • @Severity One said:

    Remember, they are under stress, and the infinite loop is a pretty stupid thing to do.
     

    If they're applying for a senior developer position then they should have the ability to fix stupid things under stress. The clients won't care how stupid the cause of a critical bug is -- they just want that bug fixed as soon as possible.


Log in to reply