almatar interview question

Solving an mid difficulty problem