Pseudocode and Problem Solving: Tracing, Bitwise Logic and Complexity
English · Free on iExamWorld
What pseudocode questions test
Capgemini, Accenture and several other recruiters use pseudocode questions to check logic without tying you to one language. You see a few lines written in a neutral style and must find the output, the number of iterations, or the time complexity. The syntax is simple, but the questions punish careless tracing. This note gives a reliable method and the patterns that appear most often.
Reading the convention
Every paper states or implies its own convention. A typical one looks like this:
- Integer a, b declares integer variables; Set a = 5 assigns a value.
- for (each i from 1 to 3) runs with i = 1, 2 and 3, including both ends.
- mod gives the remainder, and / between integers is integer division, so 17 / 5 is 3.
- &, | and ^ are bitwise AND, OR and XOR; << and >> are left and right shifts.
- Print writes a value; some papers print values separated by spaces.
Read the convention before you start, especially the meaning of ^. In these questions it is XOR, not a power.
A reliable tracing method
- List every variable with its starting value.
- Run one iteration at a time and write the new values in a row of a table.
- Check the loop condition before each iteration, not after.
- Watch for statements that use a value changed earlier in the same iteration.
Example: Integer a = 5, b = 3; for (each i from 1 to 3) { a = a + b; b = a - b }; Print a + b. The second statement uses the new a, so (a, b) goes to (8, 5), then (13, 8), then (21, 13), and the output is 34. Ignoring the order inside the body is the most common mistake.
Bitwise operators
Convert small numbers to binary and work column by column.
| Operation | Rule | Example | Result |
|---|---|---|---|
| AND (&) | 1 only if both bits are 1 | 6 & 3 (110 and 011) | 2 |
| OR (|) | 1 if either bit is 1 | 6 | 3 | 7 |
| XOR (^) | 1 if the bits differ | 6 ^ 3 | 5 |
| Left shift (a << k) | Multiply by 2 for each place | 3 << 2 | 12 |
| Right shift (a >> k) | Divide by 2 for each place, dropping the remainder | 20 >> 2 | 5 |
Useful facts for non-negative integers:
- n & 1 is 1 when n is odd and 0 when n is even.
- x ^ x is 0 and x ^ 0 is x.
- a = a ^ b; b = a ^ b; a = a ^ b swaps a and b without a third variable.
- A loop that adds n & 1 and then shifts n right by 1 counts the 1 bits; for 45, which is 101101, the count is 4.
- For two numbers, (x & y) + (x | y) equals x + y, which gives a fast cross-check.
Nested loops
For nested loops, count inner iterations for each outer value and add them up. If the inner loop runs from j = i to 4 while i goes from 1 to 4, the counts are 4, 3, 2 and 1, so the body runs 10 times. A break inside the inner loop ends only that inner loop; the outer loop continues with its next value. For example, with i from 1 to 5 and an inner loop over j that breaks when j > i and otherwise adds j to s, each outer pass adds 1 + 2 + ... + i, so s ends at 1 + 3 + 6 + 10 + 15 = 35.
While loops
A while loop checks its condition first, so it may run zero times. Digit-processing loops are very common:
- n mod 10 gives the last digit, and n / 10 removes it.
- r = r * 10 + n mod 10 builds the reverse of n; reversing 2580 gives 852, because a leading zero has no value.
- s = s + n mod 10 builds the digit sum.
For loops where two variables change, such as p = p - q; q = q + 1 while p > q, write each pair: starting from (10, 4), the values go to (6, 5) and then (1, 6), where the condition fails.
Recursion tracing
Draw the calls as a tree or a stack. Identify the base case first, then expand each call until it reaches the base case, and finally combine the results on the way back.
- fun(n) returning n + fun(n - 2) with base case n <= 0 gives fun(9) = 9 + 7 + 5 + 3 + 1 = 25.
- f(a, b) returning a when b is 0 and f(b, a mod b) otherwise is Euclid's algorithm for the GCD; f(84, 36) goes to f(36, 12) and then f(12, 0), giving 12.
- When a function calls itself twice, as in fun(n - 1); Print n; fun(n - 1), the number of prints doubles at each level; fun(3) prints 1 2 1 3 1 2 1, seven values in all.
If the print comes before the recursive call, values appear in the order of the calls; if it comes after, they appear in reverse.
Arrays
Note whether indexes start at 0 or 1 in the given pseudocode. For a loop that adds even elements and subtracts odd ones over {3, 8, 1, 9, 4}, the even part is 12 and the odd part is 13, so the result is -1. Keep a running total in your table rather than doing it mentally.
Time complexity
Big-O describes how the number of steps grows with the input size n, ignoring constants and smaller terms.
| Loop pattern | Complexity |
|---|---|
| Single loop from 1 to n | O(n) |
| Loop variable doubled or halved each pass | O(log n) |
| Two independent nested loops, each up to n | O(n²) |
| Inner loop from 1 to i inside a loop from 1 to n | O(n²), since the total is n(n + 1)/2 |
| Outer loop doubling, inner loop up to n | O(n log n) |
| Fixed number of steps independent of n | O(1) |
Loops that are one after another add their costs, and the larger term wins: an O(n) loop followed by an O(n²) loop is O(n²) overall. Nested loops multiply.
Sorting and searching steps
Bubble sort
In each pass, adjacent pairs are compared from left to right and swapped if they are in the wrong order. After the first pass of an ascending sort, the largest element is at the end. For [5, 1, 4, 2, 8], the first pass gives [1, 4, 2, 5, 8]. An array of n elements needs at most n - 1 passes, and the worst-case time is O(n²).
Selection sort and insertion sort
Selection sort finds the smallest remaining element in each pass and swaps it into position. Insertion sort takes the next element and shifts it left into its place in the sorted part. Both are O(n²) in the worst case.
Binary search
Binary search works only on a sorted array. Compute mid = (low + high) / 2 with integer division, compare, then set low = mid + 1 or high = mid - 1. Searching for 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] examines 16, then 56, then 23. Its time complexity is O(log n), compared with O(n) for linear search.
Flowchart logic
Standard symbols: an oval marks start and end, a parallelogram is input or output, a rectangle is a process step, a diamond is a decision with Yes and No exits, and arrows show the flow. A loop in a flowchart is simply an arrow that returns to an earlier decision. To trace one, treat it exactly like a while loop: for N = 6, F = 1, I = 1, multiplying F by I and increasing I while I <= N gives 720, the factorial of 6.
Quick revision points
- Read the stated convention first, especially for ^, mod and integer division.
- Trace with a table, one row per iteration, using updated values in order.
- for (each i from 1 to n) includes both ends.
- ^ means XOR in pseudocode questions, not a power.
- Left shift by k multiplies by 2 to the power k; right shift by k divides and drops the remainder.
- n & 1 tests odd or even; three XORs swap two values.
- break exits only the innermost loop.
- n mod 10 gives the last digit; n / 10 removes it.
- Find the base case before expanding any recursion.
- Halving or doubling loops are O(log n); dependent nested loops up to n are O(n²).
- Sequential loops add, and nested loops multiply.
- After one bubble sort pass, the largest element reaches the end.
- Binary search needs a sorted array and runs in O(log n).
- Diamond means decision, parallelogram means input or output, oval means start or end.
These notes were written with AI assistance for iExamWorld, based on the official syllabus. Spotted a mistake? Tell us.
