Data Structures & AlgorithmsHardGATE CS · TCS NQT
The stock span on day i is the number of consecutive days up to and including day i with price ≤ the price on day i. Prices are [100, 80, 60, 70, 60, 75, 85]. What is the sum of the spans over all days?
- A. 14
- B. 19
- C. 28
- D. 16
Correct answer: D. 16
Use a monotonic stack of earlier indices with decreasing prices: pop while the top price is ≤ today's price, and the span is the distance to the remaining top. Counting only the days with a rise, or ignoring equal prices, changes the spans.
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
- Bars of heights [4, 2, 0, 3, 2, 5, 1, 0, 3] (width 1 each) hold rainwater between them. How many units of water are trapped in total?
- Buildings of heights [5, 3, 8, 3, 2, 7, 9, 4] stand in a row viewed from the left. A building is visible if it is strictly taller than…
- The binary matrix [[1, 0, 1, 1, 0], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]] is given. What is the area of the largest rectangle…
- Using the same kind of binary matrix, [[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]], what is the area of the…
- How many permutations of {1, 2, 3, 4, 5} can be sorted into increasing order by a single stack, when the input is read left to right and…
- Repeatedly delete any two adjacent equal letters in "abbacddcaeeaf" until no such pair remains. What is the length of the final string?