Qualcomm Interview Question

Valid parenthesis Q from leetcode.