Oracle interview question

random leetcode question in medium-hard level.