Pregunta de entrevista de Amazon

Backtracking for both LC style questions