Cracking Code Interview 4.8 (not an ideal solution)