Intel Corporation interview question

MEX question leetcode medium, follow up hard