Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time in between. Which output sequence is IMPOSSIBLE?
- A. 1 2 3 4 5
- B. 3 2 5 4 1
- C. 4 5 3 2 1
- D. 3 1 2 5 4
Correct answer: D. 3 1 2 5 4
To output 3 first, 1 and 2 must already be on the stack with 2 above 1, so 1 cannot come out before 2. The other three can be produced, e.g. 3 2 5 4 1 is push 1,2,3, pop, pop, push 4,5, pop, pop, pop.
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
- Keys 15, 11, 27, 8, 12, 22, 29, 1 are inserted into a hash table of size 7 with h(k) = k mod 7 and separate chaining. What is the length…
- Keys 22, 33 and 44 are inserted in that order into an empty table of size 11 with h(k) = k mod 11 and quadratic probing hᵢ(k) = (h(k) +…
- Two sorted lists of 5 and 7 elements are merged by the standard merge procedure. What is the maximum number of comparisons it can make?
- A fixed-size sliding window of length 3 moves over [2, 1, 5, 1, 3, 2, 9, 1]. What is the largest window sum?
- How many element shifts does insertion sort perform to sort [4, 3, 2, 10, 12, 1, 5, 6] in ascending order?
- Quick sort with the Lomuto partition (pivot = last element) is run on an already sorted array of 10 distinct elements. How many…