datarekha

Reading Pseudocode & Predicting Output

GATE writes some algorithm questions in language-neutral pseudocode. The skill is patient, table-driven tracing — and watching the index base.

6 min read Intermediate GATE DA Lesson 51 of 122

What you'll learn

  • GATE pseudocode is language-neutral: 1-indexed arrays, for i = 1 to n, swap
  • Trace by maintaining a table of variable values, one row per iteration
  • Tracing nested loops and array-mutation loops step by step
  • The off-by-one trap: 1-indexed pseudocode vs 0-indexed Python, inclusive bounds

Before you start

GATE sometimes writes algorithm questions in language-neutral pseudocode (a plain-English sketch of an algorithm, not tied to any one language) rather than Python — arrays indexed from 1, loops written for i = 1 to n, an explicit swap. There is no trick here and nothing to memorise: the only skill is careful, patient tracing. The students who lose marks are the ones who trace in their head; the students who score keep a table. (It is also the exact habit that lets you debug a misbehaving loop in real code — step it by hand and watch the variables.)

Trace with a table — one row per iteration

The single technique for the whole topic: write down each variable and update its value on every pass. Take this loop (1-indexed, bound inclusive):

x = 0
for i = 1 to 4:
    x = x + i

Maintain a small table — the variable after each iteration:

iteration ix = x + ix after10 + 1121 + 2333 + 3646 + 410
Each row records x right after the update. Final answer: x = 10.

The loop runs for i = 1, 2, 3, 4 (the bound 4 is inclusive), accumulating 1 + 2 + 3 + 4 = 10. The table makes that impossible to get wrong.

Nested loops — count the inner iterations

When one loop sits inside another, the inner bound often depends on the outer variable. Trace the outer loop, and for each outer value count how many times the inner body runs:

count = 0
for i = 1 to 3:
    for j = 1 to i:      # inner bound depends on i
        count = count + 1
  • i = 1: inner runs for j = 1 → 1 time
  • i = 2: inner runs for j = 1, 2 → 2 times
  • i = 3: inner runs for j = 1, 2, 3 → 3 times

Total count = 1 + 2 + 3 = 6. (A square nest like for j = 1 to 3 inside for i = 1 to 3 would instead give 3 × 3 = 9 — read the inner bound carefully.)

Array-mutation loops — write the array after each step

When a loop mutates an array, keep the whole array in your table. A single bubble-style pass over a 1-indexed array A:

A = [5, 3, 8, 1]              # A[1]=5, A[2]=3, A[3]=8, A[4]=1
for i = 1 to 3:
    if A[i] > A[i+1]:
        swap A[i], A[i+1]
  • i = 1: A[1]=5 > A[2]=3 → swap → [3, 5, 8, 1]
  • i = 2: A[2]=5 > A[3]=8? no → unchanged [3, 5, 8, 1]
  • i = 3: A[3]=8 > A[4]=1 → swap → [3, 5, 1, 8]

The largest element has “bubbled” one step right. Tracking the full array each pass is the only way to stay correct.

How GATE asks this

GATE DA 2024 included a predict-the-output item written in pseudocode: a loop with an accumulator and a conditional, where you trace to a single final value (MCQ) or enter it (NAT). The setup varies — sums, counters, a swap inside an array loop — but the method is identical: build the trace table and read off the last row.

Quick check

Quick check

0/7
Q1Trace: x = 0; for i = 1 to 4: x = x + i. What is the final value of x?numerical answer — type a number
Q2Trace: count = 0; for i = 1 to 3: for j = 1 to i: count = count + 1. What is count?numerical answer — type a number
Q3Trace: x = 1; for i = 1 to 5: x = x * 2. What is x?numerical answer — type a number
Q4Trace: s = 0; for i = 1 to 5: if i is even: s = s + i. What is s?numerical answer — type a number
Q5A pseudocode array A is 1-indexed: A[1]=5, A[2]=3, A[3]=8, A[4]=1. After one pass of 'for i = 1 to 3: if A[i] > A[i+1]: swap A[i], A[i+1]', what is A[4]?numerical answer — type a number
Q6Which are common errors when tracing GATE pseudocode? (select all that apply)select all that apply
Q7Trace a while-loop (same table method): n = 13; c = 0; while n > 0: n = n // 2; c = c + 1. What is c when the loop ends?numerical answer — type a number

Practice this in an interview

All questions
What prompt engineering techniques should every LLM practitioner know?

The core toolkit is: system prompts (role and constraints), few-shot examples (format and tone anchoring), chain-of-thought (step-by-step reasoning), and output constraints (JSON schema, stop sequences). Combining these predictably closes the gap between a capable base model and a production-ready feature.

What makes a predicate sargable, and what are the most common ways to accidentally make a predicate non-sargable?

A sargable predicate (Search ARGument ABLE) is one the engine can evaluate using an index seek — a direct traversal to the matching key range. Predicates become non-sargable when a function or implicit cast is applied to the indexed column, forcing the engine to compute a derived value for every row before comparing.

What is chain-of-thought prompting and when does it help?

Chain-of-thought (CoT) prompting instructs the model to write out intermediate reasoning steps before producing a final answer, which improves accuracy on multi-step arithmetic, logic puzzles, and compositional questions. It is most impactful on models with at least ~10B parameters and on tasks where the answer space is large enough that guessing is hard.

Generate all subsets of a set — the power set — using backtracking.

At each step of a recursive walk through the input, you make a binary choice: include the current element or skip it. Recording the current path at every node of the recursion tree (not just the leaves) collects all 2^n subsets. A `start` index prevents duplicates by ensuring elements are only considered left-to-right.

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Related lessons

Skip to content