Data Structures & AlgorithmsVery hardProduct company OA
What is the length of the longest substring of ')()())(()())' that is a valid (balanced) parentheses string?
- A. 4
- B. 10
- C. 6
- D. 12
Correct answer: C. 6
Use a stack of indices with a sentinel for the last unmatched ')'. '()()' at indices 1–4 has length 4; the ')' at 5 is unmatched and resets the base; '(()())' at 6–11 is length 6. The two valid runs are separated by the unmatched ')', so they cannot be joined into 10. Counting matched pairs overall (5 pairs → 10) is the trap.
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
- The stock span of day i is the number of consecutive days ending at day i whose price is ≤ the price on day i. For prices [31, 27, 14, 21…
- What is the minimum number of swaps (of any two elements) needed to sort the permutation [4, 3, 1, 2, 6, 5] into ascending order?
- A stack-based decoder expands k[s] into s repeated k times (nesting allowed). What does '2[x3[y]z]q' decode to?
- Adjacent equal pairs are removed repeatedly with a stack until none remain. What is left of 'pqqprrsttsx'?
- An array is split into the MAXIMUM number of contiguous chunks such that sorting each chunk separately and concatenating them gives the…
- An extended Boyer–Moore vote keeps two candidates to find every value occurring MORE than n/3 times, then verifies them with a second…