Toptal Interview Question

Algorithm question, based on leetcode