GATE Computer ScienceVery hardGATE CS (PYQ pattern)
The stock 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. Prices are 100, 80, 60, 70, 60, 75, 85, 40, 90, 95, 50, 120. A stack of indices is used: pop while a[top] <= a[i]; span = i + 1 if the stack is empty else i - top; push i (0-indexed i). What is the sum of all spans, and how many pops are performed in total?
- A. 47 and 11
- B. 45 and 11
- C. 47 and 13
- D. 50 and 10
Correct answer: A. 47 and 11
The spans are [1, 1, 1, 2, 1, 4, 6, 1, 8, 9, 1, 12] (sum 47). Each index is popped at most once; 11 of the 12 indices are popped, and 12 - 11 remain on the stack.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- The largest rectangle in a histogram with bar heights 2, 1, 5, 6, 2, 3, 4, 1, 3 (unit widths) is found with a stack of indices; a sentinel…
- void insert(Stack s, int x) { if (empty(s) || top(s) <= x) { push(s, x); return; } int y = pop(s); insert(s, x); push(s, y); } void…
- Kahn's topological sort uses a FIFO queue seeded with all vertices of in-degree 0 in increasing order; vertices are dequeued, and…
- An undirected graph has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-1, 2-7, 7-8, 8-9, 9-7, 4-10, 10-11. Vertices are coloured by the parity of their…
- int range(Node n, int lo, int hi) { if (!n) return 0; int c = (lo <= n->k && n->k <= hi); if (lo < n->k) c += range(n->l, lo, hi); if…
- Keys 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85, 5, 22, 33, 38 are inserted in this order into an empty BST, and every…