Juniper Networks interview question

Leetcode Medium Question which I could solve