Pseudocode Practice Questions and Answers for Interviews
1. Basic Flow Control:
Q1. Write pseudocode to check if a given number is even or odd.
Answer:
BEGIN
INPUT number
IF number MOD 2 == 0 THEN
PRINT "Even"
ELSE
PRINT "Odd"
ENDIF
END
Q2. Create pseudocode to find the largest of three numbers.
Answer:
BEGIN
INPUT num1, num2, num3
IF num1 >= num2 AND num1 >= num3 THEN
PRINT "num1 is the largest"
ELSE IF num2 >= num1 AND num2 >= num3 THEN
PRINT "num2 is the largest"
ELSE
PRINT "num3 is the largest"
ENDIF
END
2. Loops and Iterations:
Q1. Write pseudocode to print the first 10 numbers in a Fibonacci sequence.
Answer:
BEGIN
SET a = 0, b = 1
PRINT a, b
FOR i FROM 1 TO 8 DO
SET temp = a + b
PRINT temp
SET a = b
SET b = temp
ENDFOR
END
Q2. Develop pseudocode to calculate the factorial of a number using a loop.
Answer:
BEGIN
INPUT number
SET factorial = 1
FOR i FROM 1 TO number DO
factorial = factorial * i
ENDFOR
PRINT factorial
END
3. Arrays and Strings:
Q1. Write pseudocode to reverse an array.
Answer:
BEGIN
INPUT array
SET start = 0, end = LENGTH(array) - 1
WHILE start < end DO
SWAP array[start] WITH array[end]
INCREMENT start
DECREMENT end
ENDWHILE
PRINT array
END
Q2. Create pseudocode to check if a string is a palindrome.
Answer:
BEGIN
INPUT string
SET reversedString = REVERSE(string)
IF string == reversedString THEN
PRINT "Palindrome"
ELSE
PRINT "Not a Palindrome"
ENDIF
END
4. Searching and Sorting:
Q1. Develop pseudocode for a binary search algorithm.
Answer:
BEGIN
INPUT array (sorted), target
SET low = 0, high = LENGTH(array) - 1
WHILE low <= high DO
SET mid = (low + high) / 2
IF array[mid] == target THEN
PRINT "Found at index mid"
EXIT
ELSE IF array[mid] < target THEN
low = mid + 1
ELSE
high = mid - 1
ENDIF
ENDWHILE
PRINT "Not Found"
END
Q2. Write pseudocode to implement the bubble sort algorithm.
Answer:
BEGIN
INPUT array
FOR i FROM 0 TO LENGTH(array) - 2 DO
FOR j FROM 0 TO LENGTH(array) - i - 2 DO
IF array[j] > array[j + 1] THEN
SWAP array[j] WITH array[j + 1]
ENDIF
ENDFOR
ENDFOR
PRINT array
END
5. Real-world Scenarios:
Q1. Create pseudocode to simulate an ATM withdrawal system.
Answer:
BEGIN
INPUT pin, withdrawalAmount
IF pin IS VALID THEN
IF withdrawalAmount <= balance THEN
balance = balance - withdrawalAmount
PRINT "Withdrawal Successful"
ELSE
PRINT "Insufficient Funds"
ENDIF
ELSE
PRINT "Invalid PIN"
ENDIF
END
Q2. Write pseudocode for calculating the total bill, including tax, at a
restaurant.
Answer:
BEGIN
INPUT billAmount, taxRate
SET tax = (billAmount * taxRate) / 100
SET total = billAmount + tax
PRINT total
END