Data Structures & AlgorithmsModerateGATE CS · TCS NQT
Process [5, 3, 6, 2, 7, 1] with a stack: before each push, pop every element that is greater than or equal to the new value. How many elements remain on the stack at the end?
- A. 2
- B. 1
- C. 3
- D. 5
Correct answer: B. 1
Each new value pops every larger-or-equal value above it, and the stack ends as just [1], so its size is 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
- How many contiguous subarrays of [1, 2, 3, 4, 6, 2] have a product exactly equal to 12?
- How many index pairs i < j in [1, 2, 3, 4, 5, 6, 7, 8] have equal parity and a[i] + a[j] <= 10?
- What is the length of the longest substring of aabbbcca in which every letter occurs at most twice?
- How many palindromic substrings of length 3 or more does racecar contain (counted by position)?
- How many distinct rotations does the string aabaab have (including itself)?
- What is the sum of all positive divisors of 28, a perfect number?