Bank of America Interview Question

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