Data Structures & AlgorithmsHardTCS Digital · Wipro pattern
The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time. Which of these pop sequences is IMPOSSIBLE?
- A. 4, 2, 3, 5, 1
- B. 3, 2, 5, 4, 1
- C. 2, 4, 3, 5, 1
- D. 1, 3, 5, 4, 2
Correct answer: A. 4, 2, 3, 5, 1
To pop 4 first, 1, 2, 3 must already be on the stack with 3 on top. The next pop can only be 3 (or a later push), never 2, which is buried under 3. The other three sequences can all be produced: e.g. 3 2 5 4 1 = push 1–3, pop 3, pop 2, push 4, 5, pop 5, 4, 1.
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
- Top-down merge sort (split until single elements, then merge) sorts an array of 13 elements. How many times is the merge step called?
- Insertion sort (shift larger elements right; one key comparison per inner-loop test) sorts [2, 4, 1, 3, 5] into ascending order. How many…
- Using a sliding window, what is the length of the longest contiguous subarray of [1, 2, 1, 3, 4, 3, 5, 3, 3] that contains at most 2…
- A chained hash table has 5 buckets, h(k) = k mod 5, and every new key is inserted at the HEAD of its chain. Keys 12, 7, 22, 3, 17, 2 are…
- The list 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 is reversed in groups of k = 3 nodes; a final group with fewer than k nodes is left as it is. What…
- A singly linked list holds 10 → 21 → 32 → 43 → 54 → 65. It is rearranged so that all nodes at ODD positions (1st, 3rd, …) come first…