Rakuten interview question

From Data Structures and Algorithm.