Code from "Elements of programming interviews, ch5"