Data Structures & AlgorithmsHardGATE CS · DSA
Stock prices over 8 days are [21, 87, 49, 49, 28, 34, 81, 65]. 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. 15
- B. 16
- C. 8
- D. 14
Correct answer: A. 15
Spans: [1, 2, 1, 2, 1, 2, 5, 1], sum 15. Equal prices count (≤), so using a strict < comparison gives 14 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
- You must check whether a singly linked list of n nodes is a palindrome using O(1) extra space. Which approach works in O(n) time?
- The intervals [4, 8], [13, 15], [5, 8], [8, 11], [12, 16], [8, 11] are sorted by start and merged (intervals that touch, like [2, 4] and…
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [17…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [2…
- Quickselect returns the k-th smallest element of an unsorted array in expected O(n) time. For [14, 4, 8, 46, 16, 34, 31, 40] and k = 3…
- How many contiguous subarrays of [6, 4, 2, 5, 6, 2, 3, 3] have a product strictly less than 30? (Positive values: a two-pointer window…