Infosys Pseudocode Practice (+1 Level)
10 Pseudocode Challenges with Detailed Step-by-Step Solutions
Question 11
Bitwise XOR LogicGiven an array where every element appears twice except one, what is the final output of val?
Integer arr[] = {4, 7, 2, 4, 9, 2, 7}
Integer val = 0
For i = 0 to length(arr) - 1
val = val ^ arr[i]
End For
Print val
Correct Answer: 9
Explanation: XORing identical numbers yields 0 ($x \oplus x = 0$), and XORing with 0 yields the number itself ($x \oplus 0 = x$). All duplicate pairs cancel out: $(4 \oplus 4) \oplus (7 \oplus 7) \oplus (2 \oplus 2) \oplus 9 = 0 \oplus 0 \oplus 0 \oplus 9 = 9$.
Question 12
RecursionHow many total function calls (including the initial call) are made for solve(3)?
Function solve(Integer n)
If n <= 1 Then
Return 1
End If
Return solve(n - 1) + solve(n - 2)
End Function
Correct Answer: 5 Calls
Execution Tree:
1. solve(3) calls solve(2) and solve(1).
2. solve(2) calls solve(1) and solve(0).
3. solve(1) and solve(0) return 1 directly.
Total calls: solve(3), solve(2), solve(1), solve(0), solve(1) = 5 calls.
Question 13
Two-Pointer ArrayWhat is the output after executing this array manipulation?
Integer arr[] = {10, 20, 30, 40, 50}
Integer left = 0, right = 4
While left < right
arr[left] = arr[left] + arr[right]
left = left + 1
right = right - 1
End While
Print arr[1]
Correct Answer: 60
Trace:
– Iteration 1: left = 0, right = 4 $\rightarrow$ arr[0] = 10 + 50 = 60. left becomes 1, right becomes 3.
– Iteration 2: left = 1, right = 3 $\rightarrow$ arr[1] = 20 + 40 = 60. left becomes 2, right becomes 2.
Loop ends. arr[1] contains 60.
Question 14
String & Hash LogicWhat is the value of count at the end of execution?
String str = "abacaba"
Integer count = 0
For i = 0 to length(str) - 1
If str[i] == str[0] Then
count = count + 1
End If
End For
Print count
Correct Answer: 4
Explanation: str[0] is 'a'. The loop counts occurrences of 'a' in "abacaba", which appears at indices 0, 2, 4, and 6 (4 times total).
Question 15
Bitwise MaskingWhat is the output of the following bit manipulation code for n = 29?
Integer n = 29
Integer mask = ~(1 << 2)
Integer result = n & mask
Print result
Correct Answer: 25
Explanation:
– 29 in binary = 11101_2.
– 1 << 2 creates a mask with bit 2 set: 00100_2 (4).
– Bitwise NOT ~(1 << 2) clears bit 2: 11011_2.
– 29 & ~4 turns off the 2nd bit of 29: $29 – 4 = 25$ (11001_2).
Question 16
Matrix TraversalWhat is the sum of the primary diagonal elements printed by this matrix loop?
Integer mat[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
Integer sum = 0
For i = 0 to 2
sum = sum + mat[i][i]
End For
Print sum
Correct Answer: 15
Explanation: The primary diagonal entries are mat[0][0] = 1, mat[1][1] = 5, and mat[2][2] = 9.
Sum = $1 + 5 + 9 = 15$.
Question 17
Advanced SearchingHow many times does the while loop execute in this modified Binary Search for key = 18?
Integer arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72}
Integer low = 0, high = 8, key = 18
Integer count = 0
While low <= high
count = count + 1
mid = (low + high) / 2
If arr[mid] == key Then
Break
Else If arr[mid] < key Then
low = mid + 1
Else
high = mid - 1
End If
End While
Print count
Correct Answer: 4 Iterations
Trace:
1. low = 0, high = 8 $\rightarrow$ mid = 4 (16 < 18) $\rightarrow$ low = 5. (Count = 1)
2. low = 5, high = 8 $\rightarrow$ mid = 6 (38 > 18) $\rightarrow$ high = 5. (Count = 2)
3. low = 5, high = 5 $\rightarrow$ mid = 5 (23 > 18) $\rightarrow$ high = 4. (Count = 3)
4. low = 5, high = 4 $\rightarrow$ Loop condition fails, terminates. Total iterations = 4.
Question 18
Sorting & InversionsHow many comparisons are made when performing Insertion Sort on an inverted array [4, 3, 2, 1]?
Integer arr[] = {4, 3, 2, 1}
Integer comparisons = 0
For i = 1 to 3
key = arr[i]
j = i - 1
While j >= 0 AND arr[j] > key
comparisons = comparisons + 1
arr[j + 1] = arr[j]
j = j - 1
End While
If j >= 0 Then
comparisons = comparisons + 1
End If
arr[j + 1] = key
End For
Print comparisons
Correct Answer: 6 Comparisons
Explanation: For a reverse-sorted array of size $N=4$, the number of comparisons in worst-case Insertion Sort is $\frac{N(N-1)}{2} = \frac{4 \times 3}{2} = 6$.
Question 19
Array RotationWhat is the value of arr[0] after right-rotating the array by 2 positions?
Integer arr[] = {1, 2, 3, 4, 5}
Integer k = 2, n = 5
For r = 1 to k
Integer temp = arr[n - 1]
For i = n - 1 DownTo 1
arr[i] = arr[i - 1]
End For
arr[0] = temp
End For
Print arr[0]
Correct Answer: 4
Rotation Steps:
– Initial: [1, 2, 3, 4, 5]
– 1st Rotation: [5, 1, 2, 3, 4]
– 2nd Rotation: [4, 5, 1, 2, 3]
Index 0 holds 4.
Question 20
Complex String LogicWhat output does this string character frequency comparison block produce?
String s = "bbbaac"
Integer freq[26] = {0}
For i = 0 to length(s) - 1
freq[s[i] - 'a'] = freq[s[i] - 'a'] + 1
End For
Integer ans = 0
For i = 0 to 25
ans = ans ^ freq[i]
End For
Print ans
Correct Answer: 0
Step-by-Step Logic:
– Frequencies: 'a' = 2, 'b' = 3, 'c' = 1.
– Non-zero values in freq array: 2, 3, 1.
– XOR calculation: $2 \oplus 3 \oplus 1 = (010_2 \oplus 011_2) \oplus 001_2 = 001_2 \oplus 001_2 = 0$.
Arjun Series Mock Test for All Assessments
Prepare for upcoming off-campus placement drives with full-length mock assessment tests, section-wise practice sets, and detailed performance analysis.
Enroll / Start Test Now