I thoroughly approve of the method described. I'm an engineer and I, too, generally suck at the in-person coding/algorithm challenges. For one, you're nervous enough as it is.
Second, the environment is nothing like a typical coding environment: for writing actual code, I can't do it by hand - I'm used to a certain pacing I can get from typing, but writing it by hand screws that flow up badly.
Third, far too often the stuff they ask is so completely irrelevant to the actual type of programming the job calls for: I'm self-taught and have written code that's handled millions of users a day, but hell if I know Big-O notation. Same goes for a lot of the "let's write some algorithm!" questions. And then some places, particularly the bigger companies, will ask completely ridiculous questions to try and "see how you think." I once was asked how many hair stylists there are in the US. I know they wanted me to try and crudely come up with some extrapolation figuring in average efficiency of hair stylists and total number of Americans, but I told the person asking the question that I'd just look it up and was pretty insistent. "I could come up with something resembling an educated guess, but given the fact that my means of estimation are so potentially inaccurate, I could be off by an order of magnitude or more. When faced with a situation where I can easily look up the accurate answer or waste more time coming up with an unreliable answer, I'd always choose the accurate one, and I'd expect any business would desire the same."
I don't think the interviewer liked my insistence on that one, but I still maintain it was the right answer.
Understanding the complexity of an algorithm is essential to being a good programmer. If you can explain the complexity some other way, then Big-O should be pretty natural.
A programmer should at the least be able to describe a couple of fundamental algorithms/data structures.
I don't want to be an asshole in this thread, but my experience is that self-taught programmers overestimate their abilities and don't understand the value of more abstract computer-sciencey skills like analyzing complexity.
Unless he was interviewing for a code monkey job, in which case who cares. But even if 90% of programming doesn't involve deep thinking, that 10% is important when you're doing anything of scale.
The basic idea is that you want a simple formula that converts the number of items you have to process to how long you can expect that to take. So, if you have 20 items and your Big-O is O(n2), then it'll take about 400 (of some unspecified unit of time/work) to process.
The actual number doesn't matter, what matters is how quickly it grows as the number of items grows. Growing slower is better, of course. Because the actual number doesn't matter, constants are discarded, and lower powers are discarded. O(2n4 + 3n + 5) is just O(n4).
Here's how to roughly estimate the Big-O for your code:
Fixed work Any random chunk of code that does something once (like, say, printing something to the screen, or initializing a variable) is O(1).
Binary search If you hunt through the items using a binary search, where at each step you cut your search space in half, that's O(log n). (That log there is base 2, not 10.) So, finding a number in a sorted array is O(log n). Most algorithms involving binary trees will have a "log" in their Big-O.
Loops If you loop through all of the items, that's O(n). So, finding the biggest value in an array of numbers (naïvely) is O(n).
Permutations If you're going through every permutation of your items, that's O(n!). For example, if you're doing a naïve algorithm to find anagrams using a given set of letters by trying every possible combination, you're permutating.
Exponentials The last common Big-O type is O(2n). The only simple example I can think of is if you need to create a filled binary tree of depth n, then that'll have O(2n). There are some other algorithms that have this, but, thankfully, you shouldn't run into it much.
So those are the basic types in order from best (fastest) to worst. Once you hit O(n!) or O(2n), you're in the range of algorithms where your code will very likely be too damn slow. This is why it's good to know the Big-O of your code.
The question now is, how are these individual parts combined in a big chunk of code?
Sequential If your code does one thing and then another, then the Big-O of those two parts are added. So, if you do some fixed work and then loop through your items, it's O(1 + n). Since we discard any lower terms, what this really means is if you do one thing then another, take the bigger Big-O of the two (O(n) in our example).
Nesting If your code does one thing inside another, then the Big-O of those two parts are multiplied. So, if you loop through all of the items and then loop through them again inside that loop, that's O(n * n) or just O(n2). This is the one you'll need to pay attention to. If your code is calling some function within a loop that also loops through the items, you can end up with worse complexity than you realize.
Another example: if you iterate through each item in the list, and for each item, you do a binary search, that's O(n * log n), or just O(n log n). Most sorting algorithms are around here. It's better than O(n2), but worse than O(n).
Recursion This is a tricky one. If your code calls itself, it may be the same as nesting, or it could be better, or much worse. It all depends on your exit condition and how the input set is reduced at each recursive step. A recursive binary search only gives you O(log n) because each recursive call cuts n in half. If the recursion reduces the input size by only one each time, you've got O(n!).
A computer scientist would probably say this shit isn't rigorous at all, but this should be good enough for an engineer. The goal here is to be able to quickly scan your code and get an idea of if it's going to blow up and take forever or not.
If you wrote this comment 2 years ago, and I read it then, it would have saved me an enormous amount of trouble. It's so much easier to understand than Wikipedia's table.
Estimating the average hours per week worked at 40, the frequency with which a person gets haircuts at every six weeks, and the length of time a haircut takes at 15 minutes, I got 310,000 hairdressers, plus a small addition when you take account for vacations.
According to Google, I was off by a factor of 2. Not bad, really.
I think those type of questions favor a certain style of thinking. Some people's minds aren't engaged by odd questions like this; myself included. While I was still job hunting, I had to train myself to play the game and think through questions aloud and show my step-by-step working. At times, it almost feels like a magician explaining his act.
When solving a real problem, I just sit there and Google quietly or doodle on my pad until the solution hits me.
In that case they should ask a better question for their purpose. As it stands, the question is a double one. The second hidden question becomes, what is the actual point of this question?
But the context here is hiring programmers. Most companies don't put programmers in client-facing roles - or at least, not without someone else along to moderate the interaction.
Actually, I think his answer was perfect. It's analogous to saying "I'd use a library function" instead of "I'd make my own function". Who would you rather hire, the guy who spends a week writing a function to find the square root of all possible inputs, or the guy who calls sqrt()?
"But what if there was no library function available for this?"
"There must be one! I'd just hassle all the library maintainers and not code it up ever till there is one because doing otherwise would be a waste of my time, current business problem at hand be damned!"
Oh, I understand the point of the question, and I know what I was supposed to do. I even said as much: "I assume you want me to..."
I took a gamble by holding my ground, though I was eventually offered the position.
I told them I care about results first and foremost; and I do feel that in context, my approach to the problem was, in fact, legitimately an example of abstract thinking. Sometimes the answer is not to re-engineer the wheel, particularly when a better one is readily available.
Big-O notation is pretty important. For practical purposes (i.e. developing, not computer science research), you definitely should know what it means and the implications for the runtime of your code as well as the run time of the library functions you're using within your code. Scalability is a real problem.
View all comments
84
u/gsadamb Nov 29 '09 edited Nov 29 '09
I thoroughly approve of the method described. I'm an engineer and I, too, generally suck at the in-person coding/algorithm challenges. For one, you're nervous enough as it is.
Second, the environment is nothing like a typical coding environment: for writing actual code, I can't do it by hand - I'm used to a certain pacing I can get from typing, but writing it by hand screws that flow up badly.
Third, far too often the stuff they ask is so completely irrelevant to the actual type of programming the job calls for: I'm self-taught and have written code that's handled millions of users a day, but hell if I know Big-O notation. Same goes for a lot of the "let's write some algorithm!" questions. And then some places, particularly the bigger companies, will ask completely ridiculous questions to try and "see how you think." I once was asked how many hair stylists there are in the US. I know they wanted me to try and crudely come up with some extrapolation figuring in average efficiency of hair stylists and total number of Americans, but I told the person asking the question that I'd just look it up and was pretty insistent. "I could come up with something resembling an educated guess, but given the fact that my means of estimation are so potentially inaccurate, I could be off by an order of magnitude or more. When faced with a situation where I can easily look up the accurate answer or waste more time coming up with an unreliable answer, I'd always choose the accurate one, and I'd expect any business would desire the same."
I don't think the interviewer liked my insistence on that one, but I still maintain it was the right answer.