Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA
What is the length of the longest well-formed (balanced) contiguous substring of ")()())"?
- A. 2
- B. 6
- C. 4
- D. 8
Correct answer: C. 4
A stack that stores indices of unmatched ')' gives the longest valid run as current index minus stack top. The string contains the balanced block "()()" of length 4 after the leading ')'.
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
- n = 7 people stand in a circle numbered 1 to 7. Starting from person 1, every 3rd remaining person is removed until one remains. Which…
- A linked list holds 1, 2, 3, 4, 5, 6, 7. Reversing each complete group of 3 nodes (leaving a final incomplete group unchanged) gives which…
- How many integers from 1 to 100 inclusive are divisible by at least one of 3, 5 or 7?
- What is the sum of lcm(a, b) over all unordered pairs of distinct positions in [2, 3, 4, 6]?
- How many non-empty subsequences of [2, 3, 5, 7] have a product strictly less than 50?
- In how many ways can [1, 2, 3, 0, 3] be cut into 3 non-empty contiguous parts such that all three parts have equal sum?