Amazon Interview Question

coding question with time complexity analysis