Amazon Interview Question

Problem similar to something in cracking the coding interview