Pregunta de entrevista de Tata Consultancy Services

it was a medium level leetcode question.