Data Structures & AlgorithmsVery hardGATE CS · Product placement
Items P, Q, R, S, T, U are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- A. Q S R P U T
- B. P Q R S T U
- C. T S R U Q P
- D. S R Q U P T
Correct answer: D. S R Q U P T
Simulate greedily: push until the needed item is on top, then pop. In S R Q U P T, when P must be popped, T is on top of it (pushed later and not yet popped), so P 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
- How many contiguous subarrays of [7, 6, 8, 5, 9, 1, 1, 9, 1, 9] contain exactly 2 odd numbers? (Use exactly(k) = atMost(k) − atMost(k − 1).)
- Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is…
- What does this loop-counting code print? (Python) python n, c = 20, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 5, 1, 2, 3, 7, 4]. Treating i → a[i] as a linked…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…
- Trains occupy a platform from arrival to departure INCLUSIVE, so a train arriving at the same minute another departs needs a separate…