Solved amazon question recursively.