Cognizant interview question

Coding challenge was given to be solved.