Amazon interview question

Coding challenge LC Medium. Interview LC Easy