Data Structures & AlgorithmsVery hard
Starting from an empty stack, perform in order: push 4, push 9, pop, push 2, push 7, pop, pop, push 5. Reading from the bottom to the top, what is the contents of the stack at the end?
- A. 5 4
- B. 4 5
- C. 4 2 5
- D. 9 5
Correct answer: B. 4 5
Track the stack contents exactly: pops remove the most recent survivor, so the final contents from bottom to top are determined by the surviving pushes. Reversing the reading order or dropping the last push gives a different sequence.
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
- Take the values of 4 9 2 7 3 8 5 6 1 10 at the ODD 0-based indices (1, 3, 5, …) in order, forming a new sequence. What is the length of…
- Split 8 3 5 1 9 2 7 4 into consecutive blocks of 4 (positions 0–3 and 4–7). For each block take (max − min), and add these two numbers…
- In 3 1 4 1 5 9 2 6 5 3 5 8, count how many positions hold a NEW strict prefix maximum (the element is strictly greater than every element…
- How many DISTINCT strings are produced by all cyclic rotations of "abcabc" (including the identity rotation)?
- How many inversions (pairs i < j with A[i] > A[j]) does 4 5 6 1 2 3 contain?
- Records are (letter, number): (b, 2), (a, 1), (c, 2), (a, 2), (b, 1), (c, 1). Apply a stable sort by LETTER first, then a stable sort by…