What is the maximum number of parenthesis that will appear on the stack at any…
2014
What is the maximum number of parenthesis that will appear on the stack at any one time for parenthesis expression given by ( ( ) ( ( ) ) ( ( ) ) )
- A.
2
- B.
3
- C.
4
- D.
5
Attempted by 447 students.
Show answer & explanation
Correct answer: B
Answer: 3
Method: Scan the expression left to right. Push on each '(' and pop on each ')', tracking the current stack depth and the maximum depth seen.
1st symbol '(' -> depth 1
2nd symbol '(' -> depth 2
3rd symbol ')' -> depth 1
4th symbol '(' -> depth 2
5th symbol '(' -> depth 3
6th symbol ')' -> depth 2
7th symbol ')' -> depth 1
8th symbol '(' -> depth 2
9th symbol '(' -> depth 3
10th symbol ')' -> depth 2
11th symbol ')' -> depth 1
12th symbol ')' -> depth 0
Maximum depth observed: 3