Data Structures & AlgorithmsHard
Why is a stack-based check that tracks TYPE as well as nesting needed for two bracket kinds?
- A. Two bracket kinds can be checked by counting alone.
- B. A closing bracket must match the most recently opened bracket of the same kind, so the stack must store which bracket was opened.
- C. Each bracket type can be checked independently of order.
- D. Type matters only for the first bracket.
Correct answer: B. A closing bracket must match the most recently opened bracket of the same kind, so the stack must store which bracket was opened.
Counting alone accepts strings such as '(]' that are not balanced, and the stack's top must match the closing kind. Storing the opened symbol makes each closing check exact.
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
- To count distinct subsequences of a string with repeated letters, which strategy avoids double counting?
- For the 'largest number from concatenation' problem, why does the comparator compare x+y with y+x as strings?
- When choosing non-adjacent positions to maximise a sum, why does the DP keep two states per position?
- Why does the lexicographically largest subsequence after deleting k digits come from a greedy stack?
- In a sliding window tracking the maximum number of vowels among windows of fixed length 3, why is O(1) update per step possible?
- Why does a circular window of length 3 require index arithmetic modulo n?