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?

  1. A. 14
  2. B. 19
  3. C. 28
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic