Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
What is the length of the longest valid (well-formed) parentheses substring of )()())?
- A. 2
- B. 4
- C. 6
- D. 8
Correct answer: B. 4
A stack of indices, with the base index set to the last unmatched ')', measures the longest run of matched pairs. Reporting the total number of matched brackets as the length is the usual mistake.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Starting from an empty deque, the operations are: push front 1, push back 2, push front 3, pop back, push back 4. Let the deque contents…
- A robot starts at position 0 on a number line. For each move it steps right (R) or left (L). Count the number of distinct positions…
- A text cursor starts after the text abc (cursor at the end). Operations in order: type x, Left, Left, Backspace, Right, type y. Here Left…
- How many integers x with 1 <= x <= 500 satisfy x mod 7 = x mod 11?
- How many ordered pairs (i, j) with 1 <= i, j <= 20 have i^2 + j^2 divisible by 5?
- How many integers x with 1 <= x <= 600 are divisible by 4 or 6 but not by 12?