Rakuten interview question

Complete a typical algorithm coding exercise