Infosys Pseudocode Questions: How to Trace Them Fast, with 17 Solved Examples

Stop tracing pseudocode in your head. Use one repeatable table for output, error and missing-line questions, with 17 solved examples you can check yourself.

KnowledgeGate Team

Exam prep & CS education

Updated 20 Jul 20266 min read

The Infosys pseudocode section is not a coding round. Treating it like one is why candidates lose marks on output, error, and missing-line questions. They trace in their heads, drop one variable update, and choose the confidently wrong option.

The fix is mechanical: write a variable-trace table and update it line by line. That one habit answers all three shapes the section sets, whether you are asked to predict the output, spot the error, or supply the missing line.

What the Infosys pseudocode section actually tests

Expect C-like pseudocode rather than code that must compile under a real language standard. The task may ask you to predict output, spot an error, or fill a blank so that the stated result appears. Syntax clues such as &, loop bounds, integer operations, and array indexes carry the answer.

Candidate reports commonly describe an adaptive flow, where a correct answer tends to lead to a harder next question. Question count, timing, and marking vary by hiring cycle and assessment route, so read the instructions on your own test screen before you start rather than working from numbers quoted second-hand.

Infosys placement preparation: test pattern, sections and a study plan places pseudocode beside the other test and interview parts.

The one technique: a variable-trace table

Draw one column for every variable that changes. Add a new row whenever a statement updates a value. For an array loop, include the index and current element. For a branch, record whether its condition was true. Never combine two iterations in your head.

For example, if s = s + arr[i] and then m may change, write the new s first and the new m second. This preserves execution order. A table takes seconds and prevents the common mistake of using a future value too early.

Example 1: trace an array scan

Integer arr[] = {3, 1, 4, 1, 5}
Integer s = 0, m = arr[0]
for i = 0 to 4 {
    s = s + arr[i]
    if arr[i] > m then m = arr[i]
}
Print s, m

i

arr[i]

s after addition

m after comparison

0

3

3

3

1

1

4

3

2

4

8

4

3

1

9

4

4

5

14

5

The sum is 3 + 1 + 4 + 1 + 5 = 14, and the largest value seen is 5. Output: 14 5.

Variable-trace table for the array scan, with columns i, arr[i], s and m. Rows: (0, 3, 3, 3), (1, 1, 4, 3), (2, 4, 8, 4), (3, 1, 9, 4) and a highlighted final row (4, 5, 14, 5). A line under the table reads Output: 14 5.

Example 2: call-by-value vs call-by-reference

Integer a = 5, b = 8
swap(a, b)
Print a, b

Procedure swap(Integer &x, Integer &y) {
    x = x + y
    y = x - y
    x = x - y
}

The & markers mean x and y refer to the caller's variables.

Step

x

y

Caller state

Entry

5

8

a=5, b=8

x = x + y

13

8

a=13, b=8

y = x - y

13

5

a=13, b=5

x = x - y

8

5

a=8, b=5

Output: 8 5. If the parameters were passed by value, only local copies would change, so the caller would still print 5 8. One symbol flips the answer.

Example 3: trace recursion and loop counts

Function f(n) {
    if n <= 1 then return 1
    return n * f(n-1)
}
Print f(4)

The calls descend before any multiplication completes:

f(4) = 4*f(3) = 4*3*f(2) = 4*3*2*f(1) = 4*3*2*1 = 24.

On return, the values are 1, 2, 6, and 24. Output: 24.

Recursion call stack for f(4). A PUSH (calls) column with downward arrows through f(4), f(3), f(2) and f(1), then a POP (returns) column with upward arrows: return 1, then 2 times 1 = 2, then 3 times 2 = 6, then 4 times 6 = 24, ending in an Output 24 box at the top.

For loop-count questions, use the same discipline. Add a count column and increment it only when the innermost statement actually runs. In for i = 1 to 3 { for j = i to 3 { count = count + 1 } } the inner loop runs three times at i = 1, twice at i = 2 and once at i = 3, so count ends at 6, not 9.

Examples 4 to 15: the same method across question types

Each row below compresses a small trace table. The values after the arrows are the successive states you should write on paper.

Array scans and string handling

#

Pseudocode idea

Trace

Output

4

Count evens in {2,5,8,9}

count: 0 -> 1 -> 1 -> 2 -> 2

2

5

Sum {6,2,1} from index 2 down to 0

s: 0 -> 1 -> 3 -> 9

9

6

Count vowels in "CODE"

i: 0,1,2,3; v: 0 -> 0 -> 1 -> 1 -> 2

2

7

Append "CAT"[i] for i=2 down to 0

out: "" -> "T" -> "TA" -> "TAC"

TAC

Sorting and searching

#

Pseudocode idea

Trace

Output

8

Linear-search 7 in {4,7,9}

i=0: no; i=1: yes

index 1

9

Binary-search 11 in {2,5,8,11,14}

(l,h,m): (0,4,2) -> (3,4,3)

index 3

10

One bubble pass on {3,1,2}

{3,1,2} -> {1,3,2} -> {1,2,3}

{1,2,3}

11

Select minimum in {5,2,4}, then swap first

min: 0 -> 1 -> 1; swap indexes 0,1

{2,5,4}

Bits, parameters, and recursion

#

Pseudocode idea

Trace

Output

12

Count set bits using n = n & (n-1), start n=12

(n,c): (12,0) -> (8,1) -> (0,2)

2

13

Compute 10 & 6

binary 1010 & 0110 = 0010

2

14

Pass a=7 by value to inc(p){p=p+1}

caller a=7; local p: 7 -> 8

caller prints 7

15

sum(n)=0 at n=0, else n+sum(n-1); call sum(3)

3+2+1+0

6

These examples cover different syntax, but the work is identical: record state, execute one line, record state again. Do not jump directly from input to an intuitive output.

Error-spotting and missing-line questions

The same table answers the other two shapes. For an error question, trace until one line tries something the data does not allow. For a missing-line question, trace with the blank left empty and ask what single update would leave the stated result.

Example 16: spot the error

Integer arr[] = {4, 9, 2}
Integer total = 0
for i = 0 to 3 {
    total = total + arr[i]
}
Print total

Trace it and the fault appears on the last row. total moves 0, 4, 13, 15 across indexes 0, 1 and 2, and then i reaches 3, where arr[3] does not exist: the array holds three elements at indexes 0, 1 and 2. The loop bound is the error, not the addition. Correct it to for i = 0 to 2 and the output is 15.

Example 17: supply the missing line

Integer n = 5, f = 1
for i = 1 to n {
    ______
}
Print f

The question states that the output is 120. Trace the blank as an unknown update: f must reach 120 after five iterations, and 120 is 5 factorial, so the missing line multiplies: f = f * i gives f as 1, 2, 6, 24, 120. The tempting alternative f = f + i is eliminated by tracing it too, because it gives 2, 4, 7, 11, 16.

Traps to mark before you calculate

  • Pre-increment vs post-increment: ++i produces the incremented value; i++ produces the earlier value before the increment takes effect.

  • Integer division: 7 / 2 becomes 3 when both operands are integers.

  • Zero-based arrays: the first element is normally index 0 in C-like questions.

  • Inclusive bounds: 0 to n often means n+1 iterations, while 0 to n-1 means n.

  • Value vs reference: look for & or explicit reference wording before tracing a procedure.

Because the flow adapts, your first few answers shape every question that follows. Trace those completely, even when the pseudocode looks trivial.

The short version and your next step

Always trace on paper. Give every changing variable a column, add a row for every update, and circle the reference marker before entering a procedure. That turns a rushed mental puzzle into bookkeeping.

Use the Infosys Superset Preparation course for structured practice and the Placement Preparation catalogue for the wider drive. For more output-prediction reps in a real language, work through Python Output-Based Questions: 13 Solved Snippets and predict each snippet before you read its explanation.