Mr. John interview question

It was based on DSA