Amazon Interview Question

Very similar to the Videos explaining this process on Youtube.