Data Structures & AlgorithmsVery hardGATE CS
The integers 1, 2, 3, 4, 5, 6 are pushed onto an initially empty stack in this order; pops may happen at any time, and the 6 popped values form an output sequence. How many distinct output sequences start with 3?
- A. 35
- B. 56
- C. 28
- D. 42
Correct answer: C. 28
Once 1, 2, 3 are pushed and 3 is popped first, 1 and 2 stay on the stack (2 on top) while 4, 5, 6 can still be pushed and popped in any valid interleaving. Enumerating all 132 valid output sequences, exactly 28 start with 3 (those starting with 1 number 42, with 2 number 42).
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 hash table of size 11 uses double hashing: the probe sequence for key k is (k mod 11 + i · (7 − (k mod 7))) mod 11 for i = 0, 1, 2…
- A hash table of size 7 uses h(k) = k mod 7 with linear probing. A delete leaves a tombstone, an insert goes into the first empty or…
- A circular queue is stored in an array of size 6 (indices 0-5). Initially empty, front = 0, and an enqueue stores at index (front + count)…
- A singly linked list holds 1, 2, ..., 11 in order. Starting from the head, every alternate group of k = 3 consecutive nodes is reversed in…
- For the array [7, 2, 4, 9, 3, 5, 1, 8, 6, 2, 10, 4, 3, 9] and window size k = 4, compute (maximum - minimum) for every window of k…
- The span of a day is the number of consecutive days ending that day (including it) whose price is at most that day's price. For prices…