Apple interview question

Write a function that passes the tests below.