Infosys · Question 48 · Stacks
Score of Parentheses
Problem
Compute score of parentheses using rules: () = 1, AB = A+B, (A) = 2×A.
Approach
Use a stack to accumulate scores. On '(' push 0, on ')' pop and compute.
Time O(n)Space O(n)
How it works
- 1'(' opens a fresh frame.
- 2')' closes it into max(2v, 1) added upward.
Sample input / output
Input: s = "(()(()))" Output: 6
Constraints
Balanced parentheses; 2 <= length <= 50.
Solution — read it, then walk it
Time O(n)Space O(n)Loops 1Branches 1Returns 2source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/17
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits