Pseudocode Questions for Placements: Solved Traces for Arrays, Strings and Loops

Learn a paper-based trace method for placement pseudocode, then apply it to bubble sort, string reversal, digit reversal and 22 more recurring question shapes.

KnowledgeGate Team

Exam prep & CS education

Updated 7 Aug 20265 min read

Pseudocode-style questions carry a named section in several service-company hiring tests, including the Cognizant, Capgemini and Accenture exams. They look like coding, so candidates try to run every line mentally and lose track after the first update. The section is really a reading and bookkeeping test.

The reliable method is to put every changing value on paper. Once you recognise the archetype and keep a trace table, the question becomes mechanical.

The trace-table discipline and four question types

Create one column for every variable that changes. For an array, record the whole array after a swap or pass. Move through the pseudocode one line at a time, and write whether each condition is true or false. Do not combine two iterations in your head.

This method handles the four common question shapes:

  1. Predict the output: trace until the program prints or stops.

  2. Find the bug: compare the intended invariant with the first row where it breaks.

  3. Fill the blank: infer the update or bound that makes the trace reach the target.

  4. Count iterations: list the actual values taken by each loop variable.

The Capgemini placement preparation guide places pseudocode inside a wider company-test plan, and Infosys pseudocode questions: 17 solved traces works the same discipline through call-by-reference, error-spotting and missing-line shapes. The bookkeeping habit transfers across all of them.

Worked example A: bubble sort

Trace this pseudocode:

arr = {5, 2, 9, 1}, n = 4
for i = 0 to n-2 {
    for j = 0 to n-2-i {
        if arr[j] > arr[j+1] then swap(arr[j], arr[j+1])
    }
}

Start with [5, 2, 9, 1].

  • For i = 0, compare 5 and 2, so they swap: [2, 5, 9, 1]. Compare 5 and 9, so no swap. Compare 9 and 1, so they swap. The pass ends at [2, 5, 1, 9].

  • For i = 1, compare 2 and 5, then 5 and 1. Only the second pair swaps, giving [2, 1, 5, 9].

  • For i = 2, compare 2 and 1. They swap, giving [1, 2, 5, 9].

The final sorted array is [1, 2, 5, 9]. The inner upper bound is n - 2 - i because one more largest value is already fixed at the right after each outer pass. With n = 4, the passes make 3, 2 and 1 comparisons, for 3 + 2 + 1 = 6 comparisons in total.

Trace table of the bubble sort passes, showing the array move from [5,2,9,1] to the sorted [1,2,5,9].

Worked example B: reverse a string in place

s = "CODE", n = 4
for i = 0 to n/2 - 1 {
    temp = s[i]
    s[i] = s[n-1-i]
    s[n-1-i] = temp
}

Integer division gives n/2 - 1 = 1, so i takes the values 0 and 1. At i = 0, swap s[0] = 'C' with s[3] = 'E'; the string becomes "EODC". At i = 1, swap s[1] = 'O' with s[2] = 'D'; it becomes "EDOC".

The output is EDOC. Stop after half the string. Continuing would swap the pairs back and undo the reversal. Notice that the indices are 0-based, while the length is 4.

Worked example C: reverse an integer with %10

n = 1234, rev = 0
while n > 0 {
    rev = rev*10 + n%10
    n = n/10
}

Division is integer division here.

Iteration

n before

n%10

rev after

n after

1

1234

4

4

123

2

123

3

43

12

3

12

2

432

1

4

1

1

4321

0

The loop stops at n = 0, so the output is 4321. The idiom n % 10 extracts the last digit, while integer n / 10 removes it. The same pair powers digit sums, numeric palindrome checks and digit counts.

Trace table for reversing 1234 with the modulo-ten loop, where rev builds to 4321 as n falls to 0.

Twenty-five solved archetypes to recognise

Rows 6, 10 and 22 are the three traced in full above, so use them to check that your own columns match. The remaining twenty-two shapes recur just as often, and each one yields to the same table: write the variable states out instead of guessing the answer.

#

Archetype and small question

Traced result

1

Linear search for 7 in [4,7,2]

Compare 4, then 7; return index 1

2

Maximum of [3,8,5]

Max changes 3 to 8; answer 8

3

Prefix sums of [2,1,4]

Running totals [2,3,7]

4

Count 2 in [2,5,2,2]

Counter changes 0, 1, 2, 3; answer 3

5

Two-sum target 9 in [4,5,1]

Store 4; at 5 the complement 4 is already stored, so the pair is 4 and 5

6

Reverse CODE in place

Two swaps produce EDOC

7

Palindrome test for LEVEL

Matching outer pairs reach the centre; true

8

Count vowels in DATA

Both A characters increment the count; answer 2

9

Frequencies in ABA

Map becomes {A:2, B:1}

10

Bubble-sort [5,2,9,1]

Pass states end at [1,2,5,9]

11

Selection-sort [3,1,2]

Move 1 first, then 2; result [1,2,3]

12

Insertion-sort [4,2,3]

States [2,4], then [2,3,4]

13

Bubble-swap count for [3,2,1]

Three inverted pairs cause 3 swaps

14

factorial(4)

4*3*2*1 = 24

15

fib(5), with F0=0, F1=1

Sequence 0,1,1,2,3,5; answer 5

16

Recursive sum(4)

4+3+2+1 = 10

17

Countdown from 3 with base 0

Prints 3 2 1, then stops

18

Diagonal sum of [[1,2],[3,4]]

Read 1 and 4; answer 5

19

Transpose [[1,2],[3,4]]

Result [[1,3],[2,4]]

20

Row sums of [[1,2],[3,4]]

Results 3 and 7

21

Add [[1,2],[3,4]] and [[4,3],[2,1]]

Every paired sum is 5

22

Reverse integer 1234

Four loop rows produce 4321

23

Bitwise 5 & 3

101 & 011 = 001; answer 1

24

Bitwise 5 | 2

101 | 010 = 111; answer 7

25

Set-bit count for 13

1101 contains 3 ones

Recognition saves time. Once you label a question as a shrinking-bound sort, two-pointer swap, accumulator, recursion tree or bit mask, you already know which columns the trace needs.

Five traps to mark before you trace, and your next step

Watch five details: n, n - 1 and n - 2 - i are different bounds; pre-increment updates before use while post-increment updates after use; 7/2 is 3 in integer pseudocode; indexing may start at 0 or 1; and a reference parameter can change the caller's object while a value parameter cannot. Mark each convention beside the trace before the first iteration.

Timing, question count and marking differ by company and by hiring cycle, so read the instruction screen on your own test rather than a figure quoted second-hand. The method does not change: name the archetype, then trace it on paper.

Use the coding-round strategy for placements to add timing and edge-case checks. Then practise under the clock in the Cognizant Superset preparation course, whose technical-assessment track carries a dedicated pseudocode module, and pick the matching company track from the Placement Preparation catalogue for Capgemini, Accenture and the other service-company exams. A trace table is slower than guessing for one line and much faster than repairing a wrong answer.