Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
Process [3, 1, 4, 1, 5, 9, 2, 6] with a stack: before pushing each value, pop while the top is smaller than it. How many pop operations happen in total?
- A. 6
- B. 7
- C. 8
- D. 10
Correct answer: A. 6
Count each time a larger value evicts a smaller top; equal values are not popped under the 'strictly smaller' rule.
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
- From the number 1, the allowed moves are x -> x+1, x -> x-1 and x -> 2x, staying within [1, 40]. What is the minimum number of moves to…
- In the list [1, 3, 4, 7, 2, 9], how many nodes come strictly before the first node whose value exceeds 5?
- How many index pairs i < j in [3, 5, 6, 1] satisfy a[i] XOR a[j] < a[i]?
- How many pairs of positions i < j in the string aabbab have s[i] = a and s[j] = b (the letters need not be adjacent)?
- What is the sum of the prefix-function values pi[0] through pi[n-1] of the string abacabab?
- What is the sum of lcm(i, i+1) for i = 1 to 9?