eBay interview question

It was a medium level leetcode code problem.