Freelancer interview question

how can solve this problem? and create loop