by sirsar · 6y ago · 0 comments
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.