Question d’entretien chez Nabla

How can we optimize the space-time complexity of this algorithm?