Freelancer Interview Question

how can solve this problem? and create loop