Data Structures & AlgorithmsVery hardGATE CS · Product placement
Items E, F, G, H, I, J are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- A. E H G I F J
- B. G E F H I J
- C. F G I H E J
- D. E G H I J F
Correct answer: B. G E F H I J
Simulate greedily: push until the needed item is on top, then pop. In G E F H I J, when E must be popped, F is on top of it (pushed later and not yet popped), so E cannot come out. The other sequences pass the simulation.
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
- Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many…
- One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j…
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- Binary search with mid = (lo + hi) // 2 (lo = 0, hi = n − 1, loop while lo ≤ hi) looks for 16 in [18, 39, 40, 44, 45, 56, 60, 62, 70, 84…
- For the sorted array [3, 3, 4, 5, 5, 7, 7, 8, 8, 8, 8, 10] (0-based), what are lowerbound(5) (first index with value ≥ 5) and…
- How many contiguous subarrays of [−2, 1, −3, −4, 8, 1, 4, −5, 6] have a sum divisible by 3? (Count equal prefix-sum remainders.)