The Triangle Exercise
Recently I had to hire someone to help me out at work, and I was running the interviews, which I'd never done before.
My two co-workers (Ruby guys) told me that when one of them had interviewing for the job, he was sent an exercise to complete. My other co-worker, who had tried the problem also, had come up with a long, convoluted solution. So when the new guy emailed back with something that was about ten lines long, they hired him.
Here's the problem: You have a triangle of numbers, like
1 4 3 1 9 7 1 1 5 9
and the question is, what is the greatest of all the sums that you can get by tracing a path of adjacent numbers from the top of the triangle and adding one number per row as you go down the rows?
For instance, in this case the answer is 20: the sum of the numbers 1, 3, 7, and 9, tracing a path down the right edge.
A text file is provided with this exercise that has 100 rows, too many to do without automating it. The answer for this text file is 732506.
Before looking at either of my co-workers' solutions, I tried it myself and thought I had come up with a pretty elegant solution using recursion and memoization to calculate all of the possible answers and store the partial ones as it goes along so it doesn't take forever. That can be found in recursive.js.
This one was a lot shorter than mine, and mostly more elegant. This was a lesson to me: recursion may seem cool, but it's not always the best idea.
And, last but not least, I made coffeescript versions of all of these, in addition to one final coffeescript file which is called bottom_to_top_reducing_with_comprehensions.coffee. It's definitely the shortest of the bunch, but it's kind of hard to read. I'm still not totally sold on coffeescript.