by 0x445442 · 6y ago · 0 comments
Yeah, my favorite "puzzle" that highlights the folly of such things is this; write a function which, given the number of participants (n), returns the number of games/matches necessary to be played in a single elimination tournament to determine a winner.
I actually asked this question in an interview once, only I was the interviewee. I had just fumbled about with all the nonsense algorithm problems that had no relevance for the job when the interviewer asked if I had any questions. I popped that one on him and to my surprise he actually took up the challenge. But since he didn't know the trick he fumbled about on some recursive goose chase.
He was constantly bemoaning how much of the research into human thinking revolved around insight problems, which you can give each subject once and at best time how long it takes for the insight to occur, instead of repeatable cognitive tasks that you could give the same subjects over and over while varying different parameters, to draw richer conclusions from.
Even without doing any math, or proofs, or having any algorithmic background, it shouldn't take "fumbling about on some recursive goose chase" to write the function from first principles:
>>> def games(players):
... if players <= 1: return 0
... if players == 2: return 1
... return 1 + 2 * games(players // 2)
...
>>> games(8)
7
If your interviewer couldn't come up with that, I wonder how well they do with real-world recursive algorithms on the job. It bears some resemblance to real-world problems I've had: groups of IPs combining into groups-of-groups-of-groups for addressing, computing permissions for teams-of-teams on the org chart, etc.[0] To me, trick questions involve some hard-to-come-by "aha" moment, like the XOR trick to swap two variables without a temp. This one doesn't seem to have that.
edit: Also, I'm not sure your solution works for 13 players.