Amazon Interview Question

LC question, around medium difficulty with follow-ups