r/programming • • May 09 '15

"Real programmers can do these problems easily"; author posts invalid solution to #4

https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k Upvotes

1.3k comments sorted by

View all comments

575

u/[deleted] May 09 '15

If you just ask questions and grade solely on the correctness of their solution, you're simply interviewing wrong.

A good technical interview requires discussion, whether it's high level, low level, or both.

Everybody makes mistakes - if you don't know that, you shouldn't be responsible for hiring. Aside from their ability to code, it's also important to assess how a candidate approaches problems, how they communicate, and how they respond when they're told they're wrong.

154

u/fenduru May 09 '15

We've turned candidates down for being overly focused on "finishing the solution". I don't need to know the solution, I just want to see how you operate.

I actually think it would be neat to have the interviewer be given the problem to solve at the same time as the candidate. This way you'd be testing how well they could work with the team, problem solving, and generally mistakes are fine if when called out you have a "oh, duh" moment rather than being clueless as to why your mistake was wrong

82

u/bonafidebob May 09 '15

I like this idea, at least for junior interviewers. Give the interviewer a sealed envelope that they open with the candidate, and solve together. It would completely short circuit the interviewers that want candidates to give the same answers they would!

4

u/jk147 May 09 '15

I don't think you even need a "sealed" questionnaire. Bring up a problem from one of your projects straight away, maybe a recent problem that stumped you or something that took a bit of work to fix. Work with the interviewee and see how they react to the problem and maybe work with them on it to get their ideas on how to approach it.

This way you can immediately see how the person will work with your team, and a project that actually is something you are currently working on. Instead of some random issue that has nothing to do with what the team is doing overall.

4

u/[deleted] May 09 '15

How about you just give the person a task from your current sprint? Free labour baby /s

4

u/jk147 May 10 '15

Why even hire anyone, just give the sprints out to everyone as take home assignments.

→ More replies (1)
→ More replies (1)

48

u/LessCodeMoreLife May 09 '15

I really enjoy interviewing that way actually. I'll pick a largeish looking commit from an OSS project about 10 minutes before the interview and we'll review it together, or we'll talk about how we might add a feature somewhere.

In addition to seeing how you actually work together, it also helps put the candidate at ease to know that you don't have a canned answer in mind. I hate to turn down people just because they don't operate well under pressure. The vast majority of what we do is far less stressful than an interview.

3

u/Zhirgoyt May 09 '15

I'd love to be interviewed by you if nothing else for the fun of it!

→ More replies (1)

8

u/[deleted] May 09 '15

I've had candidates like that too, nothing too extreme though.

That's a great idea! I might steal it... way more "real-world".

2

u/spinlock May 09 '15

When I've interviewed at companies I hated, I'd wait for them to ask me if I had any questions then I'd ask them my own technical question. I've never had an interviewer do well :/

→ More replies (1)
→ More replies (4)

14

u/tententai May 09 '15

Exactly. For example problem 5 is trivial with brute force in an "eval" language, and the number of variants to eval is not so huge. This could be a totally acceptable solution depending on the context.

I'd be happy with a candidate who doesn't immediately find the recursive solution but asks questions about this context to know if it's really needed to find a neater solution than brute force.

19

u/epicwisdom May 09 '15

I was under the impression that the recursion was brute force. Just reorganizing loops into recursion doesn't improve runtime directly.

24

u/zeug May 09 '15

Blindly applying recursion in an inappropriate language can result in a performance house of horrors.

For example, this Fibonacci function in C++ looks reasonable:

int fib(int n)
{
    if (n <= 0) return 0;
    if (n == 1) return 1;
    if (n == 2) return 1;
    return fib(n-1) + fib(n-2);
}

but it runs in exponential time with n.

The following runs in linear time with n:

int fib2(int n)
{
    if (n <= 0) return 0;
    int a = 1, b = 1, c = 1;
    for (int i = 2; i<n; i++)
    {
       c = a + b;
       a = b;
       b = c;
    }
    return c;
}

Running fib(45) took ten seconds on my laptop, fib2(45) was instantaneous.

15

u/dagamer34 May 09 '15

It's funny how Fibonacci is the example used for recursion when it's absolutely terrible performance-wise.

3

u/dccorona May 09 '15

It's a good place to start to teach about finding good places to optimize (though it often isn't used that way)...you can start to help people realize how much you're duplicating your effort by re-computing large portions of the fibonacci sequence several times, and explore better solutions from there.

→ More replies (4)

3

u/zeug May 09 '15 edited May 09 '15

I think one reason is that it is the simplest non-trivial example of something that one can do recursively. The factorial is so simple that you probably need another example.

Also, as /u/dccorona and /u/fredisa4letterword point out, this is a great way to introduce memoization. The following code expands on the example I had before:

class Fib3
{
  public:
    int compute(int n);

  private:
    std::map<int, int> memo;
};

int Fib3::compute(int n)
{
    if (n <= 0) return 0;
    if (memo.find(n) != memo.end() ) 
        return memo[n];
    else {
        if (n <= 2) {
            memo[n] = 1;
            return 1;
        }
        else {
            int result = compute(n-1) + compute(n-2);
            memo[n] = result;
            return result;
        }
    }
}

Now I can run fib3.compute(45) and get an instantaneous result.

The first simple for-loop implementation is still going to blow away Fib3 in raw performance, so recursion isn't truly helpful for this example.

But I think that the important thing is that one needs simple examples to start to gain skill in using recursion and techniques like memoization with these simple problems in order to apply them to harder problems that one would get lost in a pile of spaghetti otherwise. At least I need these examples to play with, as I'm not smart enough to learn this stuff straight from a real-world use case.

But I think that there is a reason to ask this sort of stuff on interviews, as long as it is done somewhat gently. These concepts are extremely powerful, one just has to pay a large up-front cost of time and effort to get skilled enough to use them and understand their performance.

Even if a junior engineer might not use them everyday, ultimately the really hard problems will demand them further along the career path. The big firms do want people who will one day make things like Google's BigTable or IBM's Watson, or who have the skill-set to improve them.

I also think that there is value to what we are doing here - tech companies want people who enjoy discussing this on a Saturday afternoon.

EDIT: Actually, Fib3 is ultimately better in some cases if you need to generate numbers repeatedly.

→ More replies (2)
→ More replies (8)

13

u/[deleted] May 09 '15

Or it can blow your stack causing your program to die a miserable death.

→ More replies (1)
→ More replies (10)

8

u/curiousGambler May 09 '15

It is. If anything, it'd run more slowly in most languages because of all the function calls.

→ More replies (2)
→ More replies (1)

3

u/log_2 May 09 '15

Aside from their ability to code, it's also important to assess how a candidate approaches problems, how they communicate, and how they respond when they're told they're wrong.

I think about the problem in my head and scribble some stuff on a whiteboard. I never lay it out correctly and speak my mind to an imaginary panel when I'm trying to solve something. I guess I would fail at your interview for not uttering any sounds.

The communication aspect is even worse, because you know the answer, so there is no point in both of us trying to arrive at a solution with you pretending to not know.

Programming interviews are not about testing someone, they are about trying to get the candidate to sell themselves. To bad us programmers aren't good at that.

→ More replies (5)

7

u/virnovus May 09 '15

Yeah, when asking someone to solve a programming problem, I'm more interested in seeing how they think then whether or not they get the right answer. Even stuff like what they name their variables can tell you a lot about how someone codes.

26

u/[deleted] May 09 '15 edited May 01 '17

[removed] — view removed comment

4

u/prof_hobart May 09 '15

As long as you're explaining that this is what you're doing, I'd like to think that would be fine.

→ More replies (2)
→ More replies (7)

2

u/DrMonkeyLove May 09 '15

Exactly. People who demand candidates answer these sorts of questions correctly act like they've never had a bug in something simple they wrote.

2

u/Zhirgoyt May 09 '15

I agree. I have yet to meet a programmer who doesn't make mistakes.

And granted I haven't interviewed for that many positions, but if I were to have an one that consists mostly of some sort of test I'd probably think twice about that work environment either way.

→ More replies (2)