First Derivatives interview question

Stack parentheses question from leetcode