Backbase Interview Question

Explain the algorithm you wrote