GATE Computer ScienceModerateGATE CS (PYQ pattern)
struct N { int v; int next; }; struct N a[6] = {{10,3},{20,5},{30,-1},{40,1},{50,2},{60,4}}; int i = 0, s = 0, k = 1; while (i != -1) { s += a[i].v * k; k++; i = a[i].next; } What is s, and what is the order of visited indices?
- A. 780; [0, 3, 1, 5, 4, 2]
- B. 820; [0, 1, 2, 3, 4, 5]
- C. 880; [2, 4, 5, 1, 3, 0]
- D. 820; [0, 3, 1, 5, 4, 2]
Correct answer: D. 820; [0, 3, 1, 5, 4, 2]
Follow the next fields from index 0: [0, 3, 1, 5, 4, 2]. Weighted sum is 10*1 + 40*2 + 20*3 + 60*4 + 50*5 + 30*6 = 820.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- How many structurally distinct binary search trees on keys 1..7 have key 4 at the root and height exactly 3 (a single node has height 0)?
- char s = "abracadabra"; int c[26] = {0}, i; for (i = 0; s[i]; i++) c[s[i] - 'a']++; int best = 0; for (i = 1; i < 26; i++) if (c[i] (i +…
- For n = 7 distinct keys, what is the maximum number of swaps that the bottom-up (Floyd) build-heap for a max-heap can perform, over all…
- 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…
- 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…