Data Structures & AlgorithmsModerateTCS NQT · stack basics
Problem: What is the maximum nesting depth of parentheses in '(1+(23)+((8)/4))+1'? Run the code.
Problem: What is the maximum nesting depth of parentheses in '(1+(2*3)+((8)/4))+1'? Run the code.
s = '(1+(2*3)+((8)/4))+1'
depth = best = 0
for c in s:
if c == '(':
depth += 1
best = max(best, depth)
elif c == ')':
depth -= 1
print(best)What is printed?- A. 2
- B. 4
- C. 1
- D. 3
Correct answer: D. 3
The deepest point is inside ((8)/4), which sits within the outer bracket, giving depth 3.
More Coding Interview Problems questions
- Problem: What does the Josephus recurrence J(1)=0, J(i)=(J(i-1)+2) mod i give for n = 7 (answer 1-indexed, every second person…
- Problem: How many integers in [1, 100] are divisible by both 3 and 5? Run the code. python print(sum(1 for x in range(1, 101) if x % 3 ==…
- Problem: Which statement about building a segment tree over an array of n elements is correct?
- Problem: What is the maximum product of three numbers in [-10, -10, 5, 2]? Run the code. python a = sorted([-10, -10, 5, 2])…
- Problem: What is the maximum subarray sum over all prefixes of [-2, 1, -3, 4]? Print the list of running maximum-subarray values with…
- Array a = [1, -2, 3, 2, -1, 4, -3, 2]. What is the length of the longest contiguous subarray whose sum is exactly 4?