CodePath interview question

How did you come up with the solution?