Baker Hughes Interview Question

Space time complexity