American Express interview question

Time and space complex of algorithm