A solution to the classic programming interview question