Pregunta de entrevista de SAP

LeetCode question (I think it was a leetcode medium)