AppBeat interview question

Write code for given problem.