Adobe Interview Question

Rectangular overlapping question approach and ways to optimise.