Pregunta de entrevista de Chewy

coding as per JS and JAVA