Rakuten interview question

Basic string repetition question and it's optimisation.