Rippling Interview Question

how to improve the time complexity