Most coding questions are lost through an incorrect trace, not a missing syntax fact. Correct tracing requires explicit state changes for output, mutation, recursion and complexity. One state table can track loop output, recursive array mutation and operation counts without relying on intuition.
Coding Questions for GATE CS: Identify the Job Before Tracing
Identify the requested result before tracing. Printed output, final array, recursive return and asymptotic bound need different records.
Question asks | State to record | Stop condition |
|---|---|---|
Printed output | Variables, loop control and accumulated output | The print statement executes |
Mutated array or pointer target | Shared storage after each write | The relevant call or program ends |
Recursive result | Parameters, array snapshot and pending return expression | The base case and full unwind are complete |
Time complexity | Loop values, iteration counts and call depth | The count has been summed and simplified |
The GATE 2026 official test papers and syllabus page places Programming in C, recursion, arrays and other data structures under Programming and Data Structures, and complexity under Algorithms. Treat it only as a 2026 scope anchor, not evidence for marks, question counts or later cycles.
Freeze the initial state before a trace, then execute one statement or call, record each change and stop at the requested quantity.
C Program Tracing: Build a State Table Before Doing Arithmetic
Local variables belong to a particular call. A function can mutate a passed array's underlying storage. Integer division follows operand types. continue skips the rest of the current iteration.
Use columns for step, i or call, condition, changed state, and accumulated value. Keep unchanged variables visible to avoid mentally resetting an accumulator.
With integer operands, 7 / 2 evaluates to 3. With a floating-point operand, 7.0 / 2 evaluates to 3.5. This is a type rule, not a rounding choice.
Coding Output Worked Example: Loop, continue, and an Accumulator
Trace this fragment one iteration at a time:
int a[] = {3, 1, 4, 1, 5};
int s = 0;
for (int i = 0; i < 5; i++) {
if (a[i] % 2 == 0) continue;
s += a[i] * (i + 1);
}
printf("%d", s);Step |
| Condition | Contribution |
|
|---|---|---|---|---|
1 | 0 |
|
| 3 |
2 | 1 |
|
| 5 |
3 | 2 |
| 0 | 5 |
4 | 3 |
|
| 9 |
5 | 4 |
|
| 34 |
![Execution trace for a = [3, 1, 4, 1, 5] showing each step, the skip at i = 2, and s reaching 34.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784194727167_kwqc5e.jpg)
The sum is 3 + 2 + 0 + 4 + 25 = 34, so the program prints 34. At i = 2, continue skips the rest of that iteration without resetting s.
Recursive Coding Worked Example: Array Mutation and Return Values
The recursive call shares array storage across frames while each return expression stays pending:
int f(int a[], int n) {
if (n == 1) return a[0];
a[n - 2] = a[n - 2] + a[n - 1];
return f(a, n - 1) + a[n - 1];
}
int a[] = {2, 1, 3, 2};
int answer = f(a, 4);Trace the descent first. Each call updates the array before the next call.
Call | Mutation | Array after mutation |
|---|---|---|
|
|
|
|
|
|
|
|
|
| Base case returns | Return 8 |
Then unwind the pending additions:
Returning from | Calculation | Return value |
|---|---|---|
|
| 14 |
|
| 19 |
|
| 21 |
![Recursion stack for f([2, 1, 3, 2], 4) showing array mutations and unwind returns 14, 19, 21.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784194728209_02l1ey.jpg)
The final answer is 21, with array [8, 6, 5, 2]. Reducing n by one per call gives Theta(n) time and Theta(n) auxiliary recursion-stack space.
Time Complexity Worked Example: Count Iterations Before Naming a Class
Nested loops do not automatically mean Theta(n log n) or Theta(n^2). Count this loop for n = 32:
int count = 0;
for (int i = 1; i <= n; i *= 2) {
for (int j = 0; j < i; j++) count++;
}Outer | Inner iterations | Cumulative |
|---|---|---|
1 | 1 | 1 |
2 | 2 | 3 |
4 | 4 | 7 |
8 | 8 | 15 |
16 | 16 | 31 |
32 | 32 | 63 |
Here, count = 1 + 2 + 4 + 8 + 16 + 32 = 63. For power-of-two n, the sum is 2n - 1. Otherwise, the last outer value is the greatest power of two at most n, so the sum remains within constant multiples of n. The time is Theta(n).
Sorting Algorithms: Complexity and Comparison moves from operation counts to algorithm comparisons. Graph Algorithms: BFS, DFS and Shortest Paths is the next example of counting work over vertices and edges.
Coding Question Traps: Undefined Behaviour, Boundaries, and Shortcuts
Undefined behaviour: Do not invent a fixed output for
int i = 1; printf("%d %d", i++, i++);. The two modifications ofiare unsequenced. First check whether the language defines the evaluation.Boundary error: With
n = 5, a loop fromi = 0whilei < nruns five times, forivalues 0 through 4. Replacing the condition withi <= nmakes it run six times, for values 0 through 5. Count from the actual start, condition and update.Short-circuit evaluation: With
x = 0,x != 0 && 12 / x > 2becomes false after its left operand. The right operand is not evaluated, so no division occurs.
GATE CS Coding Questions: A Three-Pass Practice Routine
Use three passes on one short fragment daily. First, write the state table and final output. Second, name the rule behind each change. Third, count operations and maximum stack depth. Remove the table only after traces are consistently correct.
A trace can reveal a recurrence, a mutation can alter a data-structure invariant, and a loop count can become an algorithm comparison. Dynamic Programming Explained: 0/1 Knapsack is a worked next step for state transitions and overlapping subproblems.
An official syllabus defines scope, while a question may ask for output, final state, a valid statement or an asymptotic bound. Verify the relevant cycle on the official GATE site instead of assuming a pattern or weightage.
Coding Questions: The Five-Line Checklist and Next Step
Before finalising an answer:
Identify the exact quantity requested.
Copy every initial value and relevant type.
Trace one statement or call at a time.
Separate recursive descent from the unwind.
Count concrete work before simplifying to Big-O or Theta.
Use GATE CS Exam Preparation as the broader course and test-series hub. For a structured wider preparation plan, GATE Guidance by Sanchit Sir is the next step.
The short version is simple: identify the job, preserve the state, and count before naming a complexity class. Correct tracing makes the final answer a consequence of the program, not a guess.




