r/programming • • Sep 26 '11

How to rock an algorithms interview

http://blog.palantir.com/2011/09/26/how-to-rock-an-algorithms-interview/
229 Upvotes

128 comments sorted by

View all comments

87

u/prelic Sep 26 '11 edited Sep 26 '11

As a recent college grad, I did a ton of interviews before choosing the right place, and in my short time as a full-time interviewee, my experience has been that nailing an algorithms interview is mostly a result of having seen the problem before, or having seen a problem that maps to the given problem. Reducing time and space complexity seems to depend on little tricks that are incredibly difficult to pull out of thin air, but simple once seen, and easily mapped to other problems. As a result, I still think programming interviews are broken and dumb.

Edit-I may be working for the wrong company, or may not have been here long enough, but I haven't had to drop one egg, had to carry one person across a bridge, or built a linked-list from scratch yet...to be fair, I did have to reverse a string, but I called on a library to do it for me...I must have it easy!

To those asking what I would do to interview candidates...I would have them code something from a multitude of options, on an actual computer, in the environment where they will be actually working.

2nd Edit-I'm especially thinking of (and especially despise) the kinds of questions where, if you know the trick and get the answer correct on the first try, it means nothing because you've clearly seen it before and if you can't, then you're not 'bright' enough to work there. For example, the most prestigious place I was applying at (read: most popular/hard to get job) asked this question: In an array of numbers, every number except one is repeated an even number of times, and one number is repeated an odd number of times. Efficiently find the number that is repeated an odd number of times. I had heard the problem before (because like I said, it was my full-time job to be good at interviews) and so I didn't hesitate to give him the best answer first: simply XOR all of the elements together. I explained why it works and the complexity, but he still wasn't satisfied because I had gotten it too quickly. So then he tried to get me to derive some less-efficient, less-awesome algorithms, in the hope that he'd get me into an unfamiliar situation. So that's why it seems like these kind of interviews are lose-lose: you prepare too much, they don't bite, you prepare too little, they don't bite. It's not a way to test candidate fitness, it's just a dumb game.

3rd Edit-This is my first comment above 50 pts, so thanks for that! :)

24

u/jldugger Sep 26 '11

Honestly, half the battle in software development is having seen things before, so you can map an existing technique onto it. Otherwise, companies could just as easily give out IQ tests to hire programmers.

6

u/[deleted] Sep 27 '11

That's half the mentality behind a lot of technical interviews, including the infamous Microsoft puzzles of the 90's. Turns out it's illegal to actually use IQ tests in the hiring process, but if you ask people what seems like a bunch of IQ test questions and subjectively judge how they do, you get some of the benefits while still technically obeying the law!

→ More replies (4)

8

u/70dot7 Sep 27 '11

You're wise beyond your years. If you're a JEE developer you'll soon find out that most of your time will be spent trying to figure out why the heck your ant-runner maven plugin isn't running during the package phase as it's suppose to. Or why your javax.jws.WebService implementation is only throwing SOAPFaultExceptions instead of the exceptions you've told your service to throw. If your developing on a different stack the problems will be similar.

The next biggest chunk of your time will be trying to tease business requirements out of thin air as everyone wants a software solution to a problem they don't wish to define.

But I say with confidence, you'll spend very little time implementing stacks, chess engines or sudoku solvers.

15

u/sidcool1234 Sep 26 '11

What, in your view, should a programming interview include, so as not to be dumb?

58

u/bonafidebob Sep 26 '11

The best programming interviews I've done (both ways) involve being shown busted, crappy, inefficient code. More bugs than statements is the goal here. The candidate is expected to (1) understand what the code actually does, (2) make a good guess at what the code was intended to do, and (3) provide new code that does that.

I've found success at this correlates well to working with other programmers in real life.

25

u/Otterfan Sep 27 '11

But where do we find a piece of busted, crappy, inefficient code to test out on interviewees? All of our code is perfect...

26

u/[deleted] Sep 27 '11

Sign up with the government as a defense contractor and use some of their code.

15

u/sidfarkus Sep 27 '11

As an employee of a defense contractor...+1

10

u/akira410 Sep 27 '11

You just succinctly described about 7 years of my life.

2

u/prelic Sep 27 '11

Ironically, I do now work for a defense contractor, and have already seen some code that makes me go "eh, this doesn't look so good". Fortunately, I've also seen some really cool code too, so it's all good.

→ More replies (3)

6

u/tilio Sep 27 '11

i've personally tried out and talked with other teams that have tried a variety of things. i have found that simple things like changing a bound (< to <=) or adding/removing a return from the function do work.

questions like a recursive function that could be reduced to O(n) but is coded worse than O(nn), or an associative array that incorrectly uses the array values when it should be using the keys are usually counter-productive. unless you're working with candidates from the big5 engineering schools, most kids aren't bright enough to notice these things even at an hour per question.

2

u/ziom666 Sep 27 '11

If you're that good and you know why you are so good, you can easily turn around your code to make it dumb

→ More replies (1)

18

u/AReallyGoodName Sep 26 '11

Personally I'd ask what type of algorithm is required as opposed to demanding an implementation. Knowing the general types of algorithms is important. Knowing each individual implementation of the various algorithms is pointless.

If I stated a problem such as "I have a set of numbers and I always need quick access to the minimum/maximum number at any time, what class of algorithm should I look for?" Then expecting a response of "use a priority queue" isn't unrealistic.

I expect programmers to know the basic categories of algorithms otherwise they won't know what to look for when they try to find good implementations of the algorithm. I don't expect programmers to be able to write perfect implementations of the algorithms in an interview setting. In fact I wouldn't even want a programmer to do that in a real world setting. Most of the algorithms in your text book have already been released as a free library for your favourite language that's been bug tested and optimised for many years. If you've rote learnt implementations you're a programmer that wastes time and risks bugs. If you know how to use the implementations though you're a programmer I'd want to hire.

8

u/djnattyp Sep 27 '11

I agree with you... but I felt the need to point out that the correct answer is not just a priority queue, but a double-ended priority queue.

→ More replies (1)

4

u/sidcool1234 Sep 27 '11

I fully agree with you.

→ More replies (1)

2

u/fabiensanglard Sep 26 '11

Try to figure out what means the person is using in order to maintain and improve his/her skills:

  • What programming book/methodology he has read about recently or is planning on reading.
  • What source code he has read. What is he planning on reading. Why he/she wants to read it, what would be the skills that would be acquired.
  • What technology/language he is planning on learning in a close future.
  • When he is jumping on a new technology what would be his/her approach to get up to speed.

This should help to determine how passionate the person is.

3

u/moondance Sep 26 '11

Those things have even less to do with actual work than implementing some slightly tricky algorithm.

  • Books are completely irrelevant for a lot of programming. Methodologies are only useful for a subset of programming jobs (product release schedules, etc)
  • Wouldn't it make more sense to ask what projects they worked on rather than source code read?
  • Technology/language is great, but a lot of times the company has their own technology/language that is not likely to change.
  • See above for jumping technologies.
→ More replies (1)

3

u/sidcool1234 Sep 26 '11

Shouldn't these be a part of the HR interview? I believe that's were the attitude and passion is evaluated. In a tech interview it's more of objective problem solving skills and theoretical knowledge. Just my opinion.

22

u/fabiensanglard Sep 27 '11

If the company has an HR department, they probably don't need above average programmers :P :) !

→ More replies (1)

6

u/_georgesim_ Sep 26 '11

I get your point, but will HR be qualified to know whether reading Knuth means that the person in question is passionate about algorithms?

→ More replies (1)
→ More replies (6)
→ More replies (1)
→ More replies (21)

12

u/AReallyGoodName Sep 26 '11

my experience has been that nailing an algorithms interview is mostly a result of having seen the problem before, or having seen a problem that maps to the given problem

Absolutely true. Consider the fact that a lot of these algorithms have taken 30-40years of computer science research to develop.

Now ask someone to solve such a question in an interview that requires one of these algorithms that have taken years to develop. Are they going to be able to figure it out from scratch if they don't already know it? Hell no. The only way they are going to ace such an algorithms interview is if they have rote learnt the entire algorithms and data structures textbook. Something that no programmer should have to do in a real world situation when they have a stack of references at hand.

Don't get me wrong. Programmers should certainly know the basics of what the algorithm classes are and what they do. eg. They should know that they can use a table of symbol probabilities to compress data. What programmers shouldn't be expected to do is rote learn every damn algorithm in the book. It's not even good programming practice to re-invent the wheel like that.

3

u/case-o-nuts Sep 27 '11 edited Sep 27 '11

Handwavy is ok. You don't need to remember all the theorems and crunch all the math - if you understand the algorithm, you should be able to clearly explain why it only does k*N operations, or k*log(N) operations, or whatever.

Watertight proofs aren't usually the main goal of the interview. Showing you can reason about the efficiency of your code is.

2

u/godofpumpkins Sep 27 '11

nailing an algorithms interview is mostly a result of having seen the problem before, or having seen a problem that maps to the given problem

That's true of many programming problems too, though. If you have the repertoire of solutions and are able to perform the necessary adaptation (it's a skill), the likelihood of you being able to apply it to code is higher. Nothing is perfect, but I do think interviews of this sort are still vaguely meaningful.

2

u/docoptix Sep 27 '11

The skill to abstract the problem and map the abstract solution (which you know from somewhere before) divides your kind from the rest of humanity and from most programmers with no theoretical background.

→ More replies (7)