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.
- A. 17
- B. 16
- C. 13
- 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.
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
- A queue is built from two stacks: enqueue pushes onto the inbox stack, and dequeue pops from the outbox stack, first transferring all…
- How many non-empty distinct palindromic partitions, i.e. ways to split the string aabb into contiguous pieces where every piece is a…
- How many distinct non-empty subsequences (as strings) does the word aba have?
- A print queue holds jobs with priorities [2, 1, 3, 2] (index 0 to 3). The front job prints only if no waiting job has a higher priority…
- A stack machine runs PUSH 3, PUSH 4, ADD, PUSH 5, MUL in order. The result is the value on top of the stack at the end. What is it?
- How many index pairs i < j in the array [1, 3, 1, 2, 3, 3] have a[i] = a[j]?