Microsoft interview question

A hard question from leetcode