bp Interview Question

Algorithm question with complexity analysis