Data Structures & AlgorithmsHard
How many different valid sequences of PUSH and POP operations are there for a stack that performs exactly 6 pushes and 6 pops in total, such that the stack is never popped when empty and ends empty?
- A. 2048
- B. 69
- C. 42
- D. 132
Correct answer: D. 132
Each valid sequence is a balanced Dyck word, so the count is the Catalan number C(n) with n = 6, which is 132. Counting all 2^(2n) operation strings ignores the emptiness constraint, and the Catalan number for n−1 is a different instance.
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
- How many contiguous subarrays of 3 1 2 4 2 3 1 2 have a sum EXACTLY equal to twice their length?
- What is the maximum sum of a contiguous subarray of 4 2 4 5 6 2 8 that contains NO repeated value?
- How many contiguous subarrays of length at least 2 of 1 1 2 2 2 3 1 1 1 1 consist of ONE repeated value?
- Treat 5 -2 3 4 -6 1 as a CIRCULAR array. What is the largest sum of three consecutive (circular) elements?
- How many index pairs i < j in 1 4 1 4 4 2 1 2 2 hold EQUAL values and satisfy j − i ≤ 2?
- How many index triples i < k < j in 2 4 6 3 9 5 satisfy A[i] + A[j] = 2·A[k] (the middle value is the arithmetic mean of the outer two)?