C++ solution to Cracking Coding Interview