Data Structures & AlgorithmsModerateGATE CS · TCS NQT

What is the sum of the stock span values for prices [100, 80, 60, 70, 60, 75, 85]? The span of day i is the number of consecutive days ending at i (including i) whose price is at most the price on day i.

  1. A. 17
  2. B. 16
  3. C. 13
  4. D. 20

Correct answer: B. 16

The spans are 1, 1, 1, 2, 1, 4, 6, found by a decreasing monotonic stack. The span counts days with price less than or equal to today, so equal prices extend the span.

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

Exams that ask this topic