CNN interview question

Algorithm question in JavaScript.