AppBeat Interview Question

Write code for given problem.