Oracle Interview Question

random leetcode question in medium-hard level.