Epic interview question

A leetcode medium hard question