A google and amazon interview question with a really clever solution