Pregunta de entrevista de IBM

Using the code below add this functionality to it. Can it be optimized further?