c2cedge
Aptitude & Reasoning · D — Programming Aptitude

Programming Logic & Pseudocode

TCS and Infosys test whether you can read code, not just write it — trace it line by line and the output is never a guess.

Test weight: High (TCS / Infosys)Time / question: 45–90 secDifficulty: Medium

Many screening tests now include a programming-logic section: a snippet of pseudocode and a question like 'what is the output?' or 'how many times does the loop run?'. You don't need to be a fast coder — you need to trace carefully, like a machine following instructions one line at a time.

Read it like a machine

Pseudocode is language-agnostic logic. The skill being tested is tracing: follow each line in order, keep a small table of variable values, and never run ahead in your head.

  • Assignment: x = 5 stores 5 in x (it is not 'equals').
  • Conditionals: if / else choose a branch based on a test.
  • Loops: for and while repeat a block.
  • Arrays: lists of values, usually 0-indexed — the first item is at index 0.
  • Operators: % is remainder, / may be integer division, == compares.
A classic: swapping two variables
a = 5
b = 7
temp = a
a = b
b = temp
// now a = 7, b = 5
LoopNumber of times it runs
for i = 1 to nn times
for i = 0; i < n; i++n times
for i = 1; i <= n; i++n times
nested: outer n, inner mn × m times
while (condition)until the condition becomes false

How to Approach It

  • Identify the construct — spot whether it's a loop, a conditional, an array access or a function call; the construct tells you what to track.
  • Build a value table — a column per variable and a row per step, updating line by line instead of holding values in your head.
  • Mind the loop boundaries — check 'less than n' vs 'less than or equal to n', and whether it counts up or down; off-by-one is the most common error.
  • Run the last iteration carefully — most mistakes hide in the final pass of a loop or at an edge value like 0.

Techniques & Methods

  • Dry-run with a table — trace on paper, one row per line of execution (e.g. for i = 1..3 doing x += i traces x as 0, 1, 3, 6).
  • Know your operators — % gives a remainder, integer / truncates, == compares, = assigns (e.g. 17 % 5 = 2; 17 / 5 = 3 integer).
  • Count loop iterations — 'for i = 1 to n' runs n times; nested loops multiply; a 'while' runs until its test fails (e.g. i = 1; while i <= 8: i = i*2 runs 4 times).
  • Remember 0-indexing — A[0] is the first element and A[n-1] is the last (e.g. A = [2,4,6]; A[2] = 6).
The Edge
Keep a value table. The single biggest cause of wrong output is tracking variables in your head. Make a column for each variable and a row for each step, and update it on every line — especially inside loops, where the last iteration is exactly where mistakes hide.
Worked example
What does this print?
x = 0
for i = 1 to 4
    x = x + i
print x
  1. Start with x = 0. The loop runs for i = 1, 2, 3 and 4 — four passes.
  2. Add each i to x in turn: 0+1 = 1, then 1+2 = 3, then 3+3 = 6, then 6+4 = 10.
  3. After the loop ends, the current value of x is printed.
Worked example
What is printed?
n = 17
if n % 2 == 0
    print 'even'
else
    print 'odd'
  1. Evaluate the test: n % 2 is the remainder when 17 is divided by 2, which is 1.
  2. The condition checks whether that remainder equals 0; since 1 is not 0, the condition is false.
  3. When the 'if' is false, the 'else' branch runs.

Worked Drills

Worked example
[Set A] Output of:
x = 0; for i = 1 to 5: x = x + i; print x
a) 10 b) 15 c) 20 d) 25
  1. 1+2+3+4+5 = 15.
Worked example
[Set A]
x = 10; x = x - 3; x = x * 2; print x
a) 14 b) 17 c) 20 d) 7
  1. (10 - 3) = 7, then 7 x 2 = 14.
Worked example
[Set A] i = 1; while i <= 8: i = i * 2. The final value of i is: a) 8 b) 16 c) 32 d) 4
  1. i: 1, 2, 4, 8, 16; the loop stops when i > 8, leaving i = 16.
Worked example
[Set A] The value of 17 / 5 in integer division is: a) 3 b) 3.4 c) 4 d) 2
  1. Integer division truncates 3.4 down to 3.
Worked example
[Set A] Sum of even numbers from 1 to 10 (add i if i % 2 == 0): a) 25 b) 30 c) 20 d) 55
  1. 2+4+6+8+10 = 30.
Worked example
[Set B]
x = 1; for i = 1 to 4: x = x * 2; print x
a) 8 b) 16 c) 4 d) 32
  1. x doubles each pass: 1 -> 2 -> 4 -> 8 -> 16.
Worked example
[Set B] for i = 1 to 4, inside it for j = 1 to i: print. Total prints: a) 8 b) 10 c) 12 d) 16
  1. Inner runs 1+2+3+4 = 10 times in total.
Worked example
[Set C] Output of:
x = 2; for i = 1 to 3: x = x * x; print x
a) 8 b) 64 c) 256 d) 16
  1. x squares each pass: 2 -> 4 -> 16 -> 256.
Worked example
[Set C]
a = 1; b = 1; repeat 3 times: c = a + b; a = b; b = c
The final value of b is: a) 3 b) 5 c) 8 d) 13
  1. Fibonacci steps: b becomes 2, then 3, then 5.
Worked example
[Set C] Reversing a number:
n = 123; rev = 0; while n > 0: rev = rev*10 + n%10; n = n/10 (integer)
The final rev is: a) 123 b) 321 c) 6 d) 312
  1. Digits pulled off and rebuilt: rev = 3, then 32, then 321.
Worked example
[Set C] A = [4,7,2,9,5]. Sum of elements greater than 4 is: a) 18 b) 21 c) 16 d) 23
  1. Elements > 4 are 7, 9, 5; 7 + 9 + 5 = 21.
Worked example
[Set C] A single loop from 1 to n doing constant work has time complexity: a) O(1) b) O(n) c) O(n^2) d) O(log n)
  1. Work grows linearly with n.
Worked example
[Set C] Number of vowels in the word INFOSYS (count I, O): a) 2 b) 3 c) 4 d) 1
  1. The vowels present are I and O — 2.
⚠ Watch out
  • Arrays are 0-indexed: the first element is index 0 and the last is index n−1.
  • % gives a remainder; integer / truncates (5 / 2 = 2, not 2.5).
  • A loop 'to n' inclusive runs one more time than a loop 'while i < n'.
Takeaways
  • Become the computer — keep a written value table, one row per executed line.
  • Watch loop boundaries (< vs <=, up vs down); off-by-one is the top error source.
  • Memorise operator behaviour: % is remainder, integer / truncates, == compares, = assigns.
Practice this — take a timed mock →
1,300+ questions, scored, with a weak-area report.
Know who's ready. Not who finished.
HomeLibraryPrivacyTerms