Amazon interview question

it was medium level leet code