Amazon interview question

LC question, around medium difficulty with follow-ups