Amazon interview question

coding question with time complexity analysis