GATE DA 2024 Q39: Trace computeS on an array
- Programming, Data Structures and Algorithms
- 2 marks
- MCQ
- −2/3 if wrong
Question
Consider the function computeS(X) whose pseudocode is given below:
computeS(X)
S[1] ← 1
for i ← 2 to length(X)
S[i] ← 1
if X[i − 1] ≤ X[i]
S[i] ← S[i] + S[i − 1]
end if
end for
return SWhich ONE of the following values is returned by the function computeS(X) for X = [6, 3, 5, 4, 10]?
Try it: multiple choice: exactly one correct option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation
Answer: C (official GATE 2024 final answer key)
Concept used: Tracing a loop: S[i] is the length of the non-decreasing run ending at position i.
First step: Compare each element with the one before it: 6>3 (reset), 3≤5 (extend), 5>4 (reset), 4≤10 (extend).
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2024 DA question paper and final answer key, organised by IISc Bangalore. Question paper · Answer key
More Programming, Data Structures and Algorithms PYQs
- 2026 · Q15Expected-time recurrence for randomised-input quicksort
- 2026 · Q16Python mutable default argument output
- 2026 · Q25Binary tree from preorder and inorder traversals
- 2026 · Q31Worst-case comparisons in binary search of 1000 elements
- 2026 · Q39Number of calls made by a Fibonacci-like recursion
- 2026 · Q40Reachability in a graph and its reverse