Data Structures & AlgorithmsVery hardTCS Digital · Product companies
The numbers 1, 2, 3, 4, 5 are pushed onto an initially empty stack in this order, and pops may be interleaved with pushes at any time. Which of the following output sequences cannot be produced?
- A. 3 2 5 4 1
- B. 4 5 3 2 1
- C. 2 3 5 4 1
- D. 3 4 1 5 2
Correct answer: D. 3 4 1 5 2
After popping 3 and 4, the stack holds 1 and 2 with 2 on top, so 2 must be popped before 1. The sequence 3 4 1 ... asks for 1 first, which is impossible; the other three sequences can be generated.
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 singly linked list holds 1 to 10 in order. Every block of k = 4 consecutive nodes is reversed in place, but a final block with fewer…
- The integers 1, 2, 3, 4, 5, 6 are pushed onto an initially empty stack in this order, and pops may be interleaved with pushes at any time…
- In a linked list the first 5 nodes form a tail, and the next 7 nodes form a cycle (the last node points back to the 6th node). Floyd's…
- For each element of the array [6, 2, 5, 4, 7, 3, 1, 8, 2] find the next greater element to its right (the value -1 if none exists). What…
- A hash table of size 11 uses h(k) = k mod 11 with linear probing. Keys 5, 16, 27 and 38 are inserted in this order, and then key 16 is…
- For the array [1, 3, -1, -3, 5, 3, 6, 7, 2, 9], consider every window of 4 consecutive elements. The sum of the maximum of each window is: