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:
Predict the output: trace until the program prints or stops.
Find the bug: compare the intended invariant with the first row where it breaks.
Fill the blank: infer the update or bound that makes the trace reach the target.
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].](https://kgai.blob.core.windows.net/blog-assets/blog_asset_1784055067651_x9w47c.jpg)
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.

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 | Compare 4, then 7; return index 1 |
2 | Maximum of | Max changes 3 to 8; answer 8 |
3 | Prefix sums of | Running totals |
4 | Count 2 in | Counter changes 0, 1, 2, 3; answer 3 |
5 | Two-sum target 9 in | Store 4; at 5 the complement 4 is already stored, so the pair is 4 and 5 |
6 | Reverse | Two swaps produce |
7 | Palindrome test for | Matching outer pairs reach the centre; true |
8 | Count vowels in | Both |
9 | Frequencies in | Map becomes |
10 | Bubble-sort | Pass states end at |
11 | Selection-sort | Move 1 first, then 2; result |
12 | Insertion-sort | States |
13 | Bubble-swap count for | Three inverted pairs cause 3 swaps |
14 |
|
|
15 |
| Sequence |
16 | Recursive |
|
17 | Countdown from 3 with base 0 | Prints |
18 | Diagonal sum of | Read 1 and 4; answer 5 |
19 | Transpose | Result |
20 | Row sums of | Results 3 and 7 |
21 | Add | Every paired sum is 5 |
22 | Reverse integer 1234 | Four loop rows produce 4321 |
23 | Bitwise |
|
24 | Bitwise |
|
25 | Set-bit count for 13 |
|
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.




