Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: What is the length of the longest valid (well-formed) parentheses substring of ")()())(()" ?
- A. 5
- B. 4
- C. 3
- D. 6
Correct answer: B. 4
A stack of indices of unmatched positions gives the length of the current valid run. Counting matched bracket pairs overall instead of a contiguous valid substring gives a different number.
More Coding Interview Problems questions
- Problem: How many strings of length 8 made of matched parentheses ( and ) are valid (well-formed)?
- Problem: A robot is on a 4×4 torus (moving off one edge reappears on the opposite edge) starting at (0,0), using row, column coordinates…
- Problem: Evaluate the postfix expression "3 4 + 2 7 −" using a stack (the operator pops the top two values, computes second-from-top op…
- Problem: Rule 30 cellular automaton on an infinite line starts with a single live cell. Each generation, a cell's new state is determined…
- Problem: How many integers x with 1 ≤ x ≤ 100 have an odd number of set bits in their binary representation AND are odd themselves?
- Problem: What is the minimum number of bit flips needed to convert 29 into 15 (flips of single bits in binary)?