I hate little puzzles like this when presented in isolation, my brain just bounces off them.
If the EXACT same puzzle was presented as part of a real problem with context and reasons, my brain would be all over it and I'd work out a solution.
As another comment says, it will inevitably show up as a coding interview, one that I would likely fail!