Amazon Interview Question

How to solve the BFQ problem