Data Structures & AlgorithmsVery hardProduct-company MCQ pattern
1, 2, 3, 4 are pushed onto a stack in this order, with pops allowed at any time. How many of the 24 permutations can be produced as the pop sequence?
- A. 24
- B. 16
- C. 14
- D. 10
Correct answer: C. 14
The valid push/pop orders are counted by the Catalan number C₄ = (1/5)·C(8, 4) = 14, and a brute-force check over all 24 orders gives the same. Ten permutations, such as 3 1 2 4, are impossible.
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
- A queue uses two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving ALL of IN into OUT only if OUT is empty. For enq 1…
- What is the postfix form of (A + B) C − (D − E) (F + G)?
- What is the value of the prefix expression − + 2 3 4 / 8 2 ?
- Infix A (B + C (D − E)) / F is converted to postfix with an operator stack (operators and '(' are pushed onto it). What is the largest…
- Using a sliding window, find the length of the longest substring with no repeated character in the string dvdfabcda.
- What is the length of the shortest contiguous subarray of [1, 4, 4, 2, 3, 1, 5] whose sum is at least 9?