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?
- A. 18
- B. 19
- C. 16
- 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.
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
- Assertion (A): Binary search runs in O(log n) time on a sorted singly linked list. Reason (R): Binary search halves the search range in…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…
- A monotonic deque reports the maximum of every window of size 3 in [0, 13, 9, 1, 2, 18, −1, −3, 17]. What is the SUM of all reported…
- An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5…
- What is the minimum number of swaps (of any two elements) needed to bring all the 1s of [1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0] together into…
- Reverse the order of words, collapsing multiple spaces. What is printed? python s = " new the is oil " w = s.split() print("|" + "…