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.
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:
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 forj = 1→ 1 timei = 2: inner runs forj = 1, 2→ 2 timesi = 3: inner runs forj = 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
Practice this in an interview
All questionsThe 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.
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.
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.
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.