Cognizant interview question

A coding task - pattern question