by ncmncm · 5y ago · 0 comments
The best online programming puzzles were the ones ITA Software posted to entice people to apply for jobs there.
What was effective about them was that, reading the problem, you wanted to know what a solution would look like, and knew the only way to know was to solve it. Having solved it, you wanted to know how your solution compared to others'. The only way to find out was to send it in. Having sent it, you might get an invitation to visit, and then how could you not?
After Google bought ITA, the puzzles were up for a while longer, then seemed to vanish. If they are archived somewhere, I would like to know where.
> Roll Your Own Chat Server
> Implement a simple standalone TCP-based chat server, using the following protocol:
> ...
> When implementing the server, aim for scalability and robustness. (Many submissions fail due to lack of robustness!)
> ...
> Please do not use an existing networking framework
Oh man. You weren't kidding, that's high-precision nerd-sniping.What I sent in was the Add-a-gram (on that page, stashed behind the "Puzzle Archive" link). I sent one solution, and when they called, they admitted others were faster, so I redid it that evening ten times faster and sent that. What they never told me, even after I had been there for years, was that my second solution was faster than theirs or any others they had on file. It looks like they retired it right after I started there, maybe because I had shown it didn't actually require backtracking, which they were trying to filter for–ITA was mostly a Lisp shop, so they wanted people who would always reach for recursion first. My solutions were relentlessly iterative.
Some years after I left ITA, they posted the Bitvectors puzzle, which I had to solve, in a fever, but never sent off. (At the time I think it had a different name.) That solution also didn't require backtracking.
And some algorithms use mutual recursion to solve problems - often putting those into a non-recursive formulation results in incredibly messy code. Mutual recursion is powerful enough that F# (among others) added an entire keyword to the language simply to support it.
And since many languages do proper tail recursion, there is no penalty for using it properly.
But the recursive code can be much shorter, and easier to understand, because our languages have built-in apparatus to support it. They didn't always; original FORTRAN could not do recursion, and used no stack. The stack isn't free at runtime, but it's pretty cheap, and its fatal failure mode is rarely encountered nowadays.
In my work, I have always found the iterative version cleaner and faster, but that is an artifact of the kinds of problems I (and most people) solve. Having the iterative version enables more flexibility in details, because everything is explicit. E.g., escaping at the end can be much faster, and patching around errors actually possible.
Computer Science education loves to dwell on recursion far beyond its real-world usefulness, in part because CS professors carry a fondness for the Lisp of their youth, but also because arbitrarily complex problems that benefit from recursion are cheap to invent and easy to describe. In real life, recursion in any pure form rarely goes, usefully, more than three levels deep, but we like to make data structures that do.
At ITA, they did airline trip computations that would naturally recurse on the number of stops, but no one wants more stops, and time spent on possible trips with more than two is almost always wasted.
In a sense, recursion is like object-orientedness: an elegant pattern that fits many problems, but makes a poor basis for a world view or, exclusively, a language. Lisp and Smalltalk are not sidelined because of ignorance.
heap(10): cons(int(1),heap(18))
heap(18): cons(int(2),heap(20))
heap(20): cons(int(3),nil)
should result in [1, 2, 3]
Here's the thing: recursive walk is totally straightforward: def restore(v):
if v.kind...
elif v.kind == 'tuple':
return Tuple([restore(v[i]) for i in range(v.arity)])
elif v.kind == 'cons':
return List(restore(v[0]), restore(v[1]))
elif v.kind...
else:
return UnknownValue()
But since Python has a limit of 1000 nested function calls, restoration of long lists fails with StackOverflow, and so I had to rewrite it using manual stack. The result is sort of ugly, in fact, I had to use two stacks: one for temporary reconstructed pieces of data, one for "what to do next". Essentially it's implementing a variation of the shunting yard algorithm and while the resulting code is kinda clear in intent, it's still three times as long as the recursive version and is not obviously correct. In fact, I had to tweak it several times (including the introduction of the second stack) before I stopped seeing some (not all!) lists reconstructed in the reverse order. def msort(xs, lo=None, hi=None):
if lo is None:
lo = 0
hi = len(xs)
if hi-lo < 2:
return
mid = lo + (hi-lo) // 2
msort(xs, lo, mid)
msort(xs, mid, hi)
merge(xs, lo, mid, hi)
def msort(xs):
s = [[0, len(xs) // 2, len(xs), "step 1"]]
while s:
lo, mid, hi, step = xs[-1]
if step == "step 1":
if hi-lo < 2:
s.pop()
else:
s[-1][3] = "step 2"
s.append([lo, lo + (mid-lo) // 2, mid, "step 1"])
elif step == "step 2":
s[-1][3] = "step 3"
s.append([mid, mid + (hi-mid) // 2, hi, "step 1"])
else:
s.pop()
merge(xs, lo, mid, hi)It's very natural for depth first search of a tree.
Nope, Linux reports it as readable/writeable but not having exceptional condition, while Windows reports it as only having exceptional condition pending but not as readable nor writeable. Beautiful, just beautiful. In the end you have to put sockets in such scenarios into all three sets and check what getsockopt(s, SOL_SOCKET, SO_ERROR) returns for them, that's the portable way.
Undergraduates solve it in two minutes; people with a BS need 5; MS 20; PhD never, or as fast as an undergraduate. The iterative solution is much simpler than a recursive one, so the question identifies how well-tuned your instincts are to problem spaces. If you go straight for recursion, you'll be there all day.
I found that everybody who found the minimal solution used the same hand gesture while describing it.
I always recited the statistics at the outset, so that the interviewee would know how much complexity to expect in the answer. It never seemed to help them.