Bank of America interview question

Can you write this or this function ? Is there a another faster way ?...