Amazon interview question

Based on ds and algorithms