Tech Mahindra Interview Question

Asked for to do code for given scenario.