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

Last lesson asked you to be the machine for a recursive function — to follow each call without guessing. This lesson widens that same discipline to every loop GATE can throw at you, and adds the one tool that makes it reliable. GATE sometimes writes algorithm questions in language-neutral pseudocode — a plain sketch of an algorithm, tied to no one language: arrays indexed from 1, loops written for i = 1 to n, an explicit swap. There is no trick 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 move, one row at a time.

Trace with a table — one row per iteration

The single technique for this 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’s value 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 — every value is written down, so nothing is held in a slippery mental register.

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, because that one difference is the whole answer.)

# Pseudocode 'for i = 1 to n' is inclusive; range(1, n+1) matches it.
x = 0
for i in range(1, 5):          # i = 1,2,3,4
    x = x + i
print("x =", x)

count = 0
for i in range(1, 4):          # i = 1,2,3
    for j in range(1, i + 1):  # j = 1..i
        count += 1
print("count =", count)

prints:

x = 10
count = 6

The triangular nest gives 6, not 9 — the inner bound j = 1 to i runs fewer times than a fixed j = 1 to 3 would.

Array-mutation loops — write the array after each step

When a loop mutates an array, keep the whole array in your table. Here is 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 after each pass is the only way to stay correct — a remembered array is a wrong array.

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 never does: build the trace table and read off the last row.

A question to carry forward

Look back at that nested loop. You counted its body running exactly 6 times for n = 3, and 9 for the square version — precise numbers, carefully traced. But now imagine n is not 3 but a million. Nobody traces a million rows, and the exact count stops being the point. What matters then is the shape of the growth: a single loop to n does about n steps, while that square nest does about — and for a million is a trillion. Here is the thread onward: how do we describe and compare how an algorithm’s work grows with the input size, ignoring the exact count but capturing whether it scales like n, like , or like log n?

In one breath

  • GATE pseudocode is language-neutral: 1-indexed arrays, for i = 1 to n (inclusive), explicit swap. The only skill is patient tracing.
  • Keep a trace table: one row per iteration, every variable’s value written down — never trace in your head.
  • Nested loop with inner bound 1 to i is triangular: 1+2+3 = 6 for n=3 (not 3×3 = 9).
  • Array-mutation loop: write the whole array after each pass (a bubble pass floats the max to the end).
  • Off-by-one is the killer: 1-indexed vs 0-indexed, inclusive to n vs Python range(1,n), and reading A[i+1] past the end.

Practice

Quick check

0/7
Q1Recall: which are common errors when tracing GATE pseudocode? (select all that apply)select all that apply
Q2Trace: x = 0; for i = 1 to 4: x = x + i. What is the final value of x?numerical answer — type a number
Q3Trace: x = 1; for i = 1 to 5: x = x * 2. What is x?numerical answer — type a number
Q4Apply: s = 0; for i = 1 to 5: if i is even: s = s + i. What is s?numerical answer — type a number
Q5Apply: count = 0; for i = 1 to 3: for j = 1 to i: count = count + 1. What is count?numerical answer — type a number
Q6Apply: a 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
Q7Create: trace a while-loop with the 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

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.

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 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.

What is Retrieval-Augmented Generation (RAG) and how does a basic RAG pipeline work?

RAG augments an LLM by retrieving relevant documents from an external knowledge store at query time and feeding them into the prompt as grounding context. A basic pipeline chunks and embeds documents into a vector store, retrieves the top-k most similar chunks for a query, and the LLM generates an answer conditioned on them, reducing hallucination and keeping knowledge current.

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.

Related lessons

Explore further

Skip to content