Amazon interview question

Time complexity analysis and improvement.