Data Structures & AlgorithmsVery hardGATE CS · DSA
What is the length of the longest contiguous substring of ((()((()(()) that is a valid (balanced) parentheses string?
- A. 6
- B. 8
- C. 12
- D. 3
Correct answer: A. 6
Use a stack of indices seeded with −1: on ')' pop, and if the stack empties push the current index as a new base, else the length is i − top. The best is 6. Counting all matched pairs (8) ignores contiguity, 12 is the whole string and 3 is the number of pairs, not the length.
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
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…
- Print a matrix in spiral order (right, down, left, up, repeat). What is printed? python m = [[90, 11, 62, 33], [40, 71, 82, 13], [10, 81…
- A 2-D array A[0..11][0..29] of 4-byte elements is stored in row-major order starting at address 1000. What is the address of A[5][11]?
- For the sorted array [1, 5, 6, 7, 7, 7, 8, 9, 9, 10, 10, 11] (0-based), what are lowerbound(9) (first index with value ≥ 9) and…
- Keys 13, 14, 24, 27, 70, 76, 91, 15 are inserted in this order into an empty hash table of size 13 with h(k) = k mod 13 and linear…