Hexaware Technologies Interview Question

Asked to solve a program in hacker rank