Rakuten interview question

Solve algorithm problem