Palo Alto Networks interview question

An algorithms and data structures question, similar to medium difficulty on LeetCode. Forced to write code with pen and paper.