Pergunta de entrevista da empresa Apple

Write a function that passes the tests below.