Data Structures & AlgorithmsHardGATE CS · Product placement

Stock prices over 8 days are [27, 32, 42, 42, 35, 66, 59, 31]. The span of day i is the number of consecutive days ending at day i (inclusive) with price ≤ the price on day i. What is the sum of all 8 spans?

  1. A. 18
  2. B. 19
  3. C. 16
  4. D. 20

Correct answer: B. 19

Spans: [1, 2, 3, 4, 1, 6, 1, 1], sum 19. Equal prices count (≤), so using a strict < comparison gives 16 instead.

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

Exams that ask this topic