OfferUp interview question

Leetcode question but it is a hard one.