[go: up one dir, main page]

0% found this document useful (0 votes)
424 views21 pages

Capgemini Pseudo Code Questions With Answer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
424 views21 pages

Capgemini Pseudo Code Questions With Answer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Pseudo Code Interview

Questions With Answers


Program 1: Reverse a String
Question: Write a program to reverse a given string.
Input: "Capgemini"

Output: "inimegpaC"

Explanation: The program should take a string and return the reverse of the
string.
Pseudo Code:

1. Take input string str .

2. Initialize an empty string reversed .

3. For each character ch in str from the end to the beginning, append ch to
reversed .

4. Return reversed .

public class ReverseString {


public static String reverse(String str) {
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
return reversed;
}

public static void main(String[] args) {


String input = "Capgemini";
String output = reverse(input);
System.out.println("Reversed String: " + output);
}
}

Pseudo Code Interview Questions With Answers 1


Program 2: Palindrome Check
Question: Write a program to check if a given string is a palindrome.
Input: "madam"

Output: true

Explanation: A string is a palindrome if it reads the same backward as forward.


Pseudo Code:

1. Take input string str .

2. Initialize two pointers left at 0 and right at str.length - 1 .

3. While left is less than right , do:

If str.charAt(left) is not equal to str.charAt(right) , return false.

Increment left and decrement right .

1. If all characters match, return true.

public class PalindromeCheck {


public static boolean isPalindrome(String str) {
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}

public static void main(String[] args) {


String input = "madam";
boolean output = isPalindrome(input);
System.out.println("Is Palindrome: " + output);
}
}

Pseudo Code Interview Questions With Answers 2


Program 3: Fibonacci Sequence
Question: Write a program to print the first n numbers of the Fibonacci
sequence.
Input: 5

Output: 0 1 1 2 3

Explanation: The Fibonacci sequence is a series of numbers where each


number is the sum of the two preceding ones.

Pseudo Code:

1. Take input integer n .

2. Initialize a to 0 and b to 1.

3. Print a and b .

4. For i from 2 to n-1 , do:

Set c to a + b .

Print c .

Set a to b and b to c .

public class FibonacciSequence {


public static void printFibonacci(int n) {
int a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}

public static void main(String[] args) {


int n = 5;
System.out.print("Fibonacci Sequence: ");
printFibonacci(n);

Pseudo Code Interview Questions With Answers 3


}
}

Program 4: Factorial Calculation


Question: Write a program to calculate the factorial of a number.

Input: 5

Output: 120

Explanation: The factorial of a number n is the product of all positive integers


less than or equal to n.

Pseudo Code:

1. Take input integer n .

2. Initialize result to 1.

3. For i from 1 to n , do:

Multiply result by i .

1. Return result .

public class FactorialCalculation {


public static int calculateFactorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}

public static void main(String[] args) {


int input = 5;
int output = calculateFactorial(input);
System.out.println("Factorial: " + output);
}
}

Program 5: Check Prime Number

Pseudo Code Interview Questions With Answers 4


Question: Write a program to check if a given number is a prime.
Input: 7

Output: true

Explanation: A number is a prime if it has only two factors: 1 and itself.

Pseudo Code:

1. Take input integer n .

2. If n is less than or equal to 1, return false.

3. For i from 2 to the square root of n , do:

If n % i is 0, return false.

1. If no divisors found, return true.

public class PrimeCheck {


public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

public static void main(String[] args) {


int input = 7;
boolean output = isPrime(input);
System.out.println("Is Prime: " + output);
}
}

Program 6: GCD Calculation


Question: Write a program to calculate the GCD of two numbers.

Input: 54, 24

Output: 6

Explanation: The greatest common divisor (GCD) of two numbers is the largest
positive integer that divides both numbers without a remainder.

Pseudo Code Interview Questions With Answers 5


Pseudo Code:

1. Take input integers a and b .

2. While b is not 0, do:

Set temp to b .

Set b to a % b .

Set a to temp .

1. Return a .

public class GCD {


public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

public static void main(String[] args) {


int a = 54;
int b = 24;
int output = gcd(a, b);
System.out.println("GCD: " + output);
}
}

Program 7: Count Vowels in a String


Question: Write a program to count the number of vowels in a given string.

Input: "Hello"

Output: 2

Explanation: Count the occurrences of vowels (a, e, i, o, u) in the string.


Pseudo Code:

1. Take input string str .

Pseudo Code Interview Questions With Answers 6


2. Initialize count to 0.

3. For each character ch in str , do:

If ch is a vowel, increment count .

1. Return count .

public class CountVowels {


public static int countVowels(String str) {
int count = 0;
for (char ch : str.toCharArray()) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch =
= 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch =
= 'O' || ch == 'U') {
count++;
}
}
return count;
}

public static void main(String[] args) {


String input = "Hello";
int output = countVowels(input);
System.out.println("Number of Vowels: " + output);
}
}

Program 8: Remove Duplicates from String


Question: Write a program to remove duplicate characters from a string.
Input: "Hello"

Output: "Helo"

Explanation: Remove all duplicate characters in the string.

Pseudo Code:

1. Take input string str .

2. Initialize result as an empty string.

Pseudo Code Interview Questions With Answers 7


3. For each character ch in str , do:

If ch is not in result , add ch to result .

1. Return result .

public class RemoveDuplicatesFromString {


public static String removeDuplicates(String str) {
StringBuilder result = new StringBuilder();
for (char ch : str.toCharArray()) {
if (result.indexOf(String.valueOf(ch)) == -1) {
result.append(ch);
}
}
return result.toString();
}

public static void main(String[] args) {


String input = "Hello";
String output = removeDuplicates(input);
System.out.println("String without Duplicates: " +
output);
}
}

Program 9: Find Largest Element in Array


Question: Write a program to find the largest element in an array.
Input:
[1, 8, 3, 7, 2]

Output: 8

Explanation: Iterate through the array and keep track of the largest element.

Pseudo Code:

1. Take input array arr .

2. Initialize max to arr[0] .

3. For each element num in arr , do:

Pseudo Code Interview Questions With Answers 8


If num is greater than max , set max to num .

1. Return max .

public class LargestElement {


public static int findLargest(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) {
max = num;
}
}
return max;
}

public static void main(String[] args) {


int[] arr = {1, 8, 3, 7, 2};
int output = findLargest(arr);
System.out.println("Largest Element: " + output);
}
}

Program 10: Find Smallest Element in Array


Question: Write a program to find the smallest element in an array.
Input: [1, 8, 3, 7, 2]

Output: 1

Explanation: Iterate through the array and keep track of the smallest element.
Pseudo Code:

1. Take input array arr .

2. Initialize min to arr[0] .

3. For each element num in arr , do:

If num is less than min , set min to num .

1. Return min .

Pseudo Code Interview Questions With Answers 9


public class SmallestElement {
public static int findSmallest(int[] arr) {
int min = arr[0];
for (int num : arr) {
if (num < min) {
min = num;
}
}
return min;
}

public static void main(String[] args) {


int[] arr = {1, 8, 3, 7, 2};
int output = findSmallest(arr);
System.out.println("Smallest Element: " + output);
}
}

Program 11: Check Anagram


Question: Write a program to check if two strings are anagrams.

Input: "listen", "silent"

Output: true

Explanation: Two strings are anagrams if they contain the same characters in
the same frequencies.
Pseudo Code:

1. Take input strings str1 and str2 .

2. If lengths of str1 and str2 are not equal, return false.

3. Sort both strings.

4. If sorted strings are equal, return true. Otherwise, return false.

import java.util.Arrays;

public class AnagramCheck {

Pseudo Code Interview Questions With Answers 10


public static boolean areAnagrams(String str1, String s
tr2) {
if (str1.length() != str2.length()) {
return false;
}
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}

public static void main(String[] args) {


String str1 = "listen";
String str2 = "silent";
boolean output = areAnagrams(str1, str2);
System.out.println("Are Anagrams: " + output);
}
}

Program 12: Merge Two Sorted Arrays


Question: Write a program to merge two sorted arrays into a single sorted
array.
Input: [1, 3, 5], [2, 4, 6]

Output: [1, 2, 3, 4, 5, 6]

Explanation: Merge two sorted arrays while maintaining the sorted order.
Pseudo Code:

1. Take input arrays arr1 and arr2 .

2. Initialize an empty array result .

3. Initialize pointers i and j to 0.

4. While i is less than the length of arr1 and j is less than the length of
arr2 , do:

If arr1[i] is less than arr2[j] , add arr1[i] to result and increment i .

Pseudo Code Interview Questions With Answers 11


Else, add arr2[j] to result and increment j .

1. Add remaining elements of arr1 and arr2 to result .

2. Return result .

import java.util.Arrays;

public class MergeSortedArrays {


public static int[] mergeArrays(int[] arr1, int[] arr2)
{
int[] result = new int[arr1.length + arr2.length];
int i = 0, j = 0, k = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
result[k++] = arr1[i++];
} else {
result[k++] = arr2[j++];
}
}
while (i < arr1.length) {
result[k++] = arr1[i++];
}
while (j < arr2.length) {
result[k++] = arr2[j++];
}
return result;
}

public static void main(String[] args) {


int[] arr1 = {1, 3, 5};
int[] arr2 = {2, 4, 6};
int[] output = mergeArrays(arr1, arr2);
System.out.println("Merged Array: " + Arrays.toStri
ng(output));
}
}

Program 13: Binary Search

Pseudo Code Interview Questions With Answers 12


Question: Write a program to perform a binary search on a sorted array.
Input: [1, 2, 3, 4, 5], 3

Output: 2

Explanation: Perform a binary search to find the index of the given key in the
sorted array.
Pseudo Code:

1. Take input array arr and integer key .

2. Initialize left to 0 and right to arr.length - 1 .

3. While left is less than or equal to right , do:

Set mid to (left + right) / 2 .

If arr[mid] is equal to key , return mid .

If arr[mid] is less than key , set left to mid + 1 .

Else, set right to mid - 1 .

1. If key is not found, return -1.

public class BinarySearch {


public static int binarySearch(int[] arr, int key) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5};
int key = 3;

Pseudo Code Interview Questions With Answers 13


int output = binarySearch(arr, key);
System.out.println("Index of Key: " + output);
}
}

Program 14: Bubble Sort


Question: Write a program to sort an array using bubble sort.
Input: [5, 3, 8, 4, 2]

Output: [2, 3, 4, 5, 8]

Explanation: Sort the array using the bubble sort algorithm.


Pseudo Code:

1. Take input array arr .

2. Initialize n to arr.length .

3. For i from 0 to n-1 , do:

For j from 0 to n-i-1 , do:

If arr[j] is greater than arr[j+1] , swap them.

public class BubbleSort {


public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

public static void main(String[] args) {


int[] arr = {5, 3, 8, 4, 2};
bubbleSort(arr);

Pseudo Code Interview Questions With Answers 14


System.out.print("Sorted Array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Program 15: Insertion Sort


Question: Write a program to sort an array using insertion sort.
Input: [5, 3, 8, 4, 2]

Output: [2, 3, 4, 5, 8]

Explanation: Sort the array using the insertion sort algorithm.


Pseudo Code:
Take input array arr .

Set key to arr[i] .

Set j to i - 1 .

While j >= 0 and arr[j] > key , do:

Set arr[j + 1] to arr[j] .

Decrement j .

Set arr[j + 1] to key .

public class InsertionSort {


public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

Pseudo Code Interview Questions With Answers 15


public static void main(String[] args) {
int[] arr = {5, 3, 8, 4, 2};
insertionSort(arr);
System.out.print("Sorted Array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Program 16: Merge Sort


Question: Write a program to sort an array using merge sort.
Input: [5, 3, 8, 4, 2]

Output: [2, 3, 4, 5, 8]

Explanation: Sort the array using the merge sort algorithm.


Pseudo Code:

1. Take input array arr .

2. If arr.length <= 1, return arr .

3. Split arr into left and right halves.

4. Recursively sort left and right .

5. Merge sorted left and right halves.

import java.util.Arrays;

public class MergeSort {


public static int[] mergeSort(int[] arr) {
if (arr.length <= 1) {
return arr;
}
int mid = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, mid);
int[] right = Arrays.copyOfRange(arr, mid, arr.leng
th);

Pseudo Code Interview Questions With Answers 16


return merge(mergeSort(left), mergeSort(right));
}

public static int[] merge(int[] left, int[] right) {


int[] result = new int[left.length + right.length];
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
result[k++] = left[i++];
} else {
result[k++] = right[j++];
}
}
while (i < left.length) {
result[k++] = left[i++];
}
while (j < right.length) {
result[k++] = right[j++];
}
return result;
}

public static void main(String[] args) {


int[] arr = {5, 3, 8, 4, 2};
int[] output = mergeSort(arr);
System.out.print("Sorted Array: ");
for (int num : output) {
System.out.print(num + " ");
}
}
}

Program 17: Quick Sort


Question: Write a program to sort an array using quick sort.
Input: [5, 3, 8, 4, 2]

Output: [2, 3, 4, 5, 8]

Pseudo Code Interview Questions With Answers 17


Explanation: Sort the array using the quick sort algorithm.
Pseudo Code:

1. Take input array arr , and integers low and high .

2. If low < high , do:

Set pi to partition arr with low and high .

Recursively quick sort arr[low..pi-1] and arr[pi+1..high] .

public class QuickSort {


public static void quickSort(int[] arr, int low, int hi
gh) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

public static int partition(int[] arr, int low, int hig


h) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}

public static void main(String[] args) {

Pseudo Code Interview Questions With Answers 18


int[] arr = {5, 3, 8, 4, 2};
quickSort(arr, 0, arr.length - 1);
System.out.print("Sorted Array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Program 18: Count Words in a String


Question: Write a program to count the number of words in a given string.
Input: "Capgemini is a global company"

Output: 5

Explanation: Count the number of words separated by spaces in the string.

Pseudo Code:

1. Take input string str .

2. Split str by spaces into an array words .

3. Return the length of words .

public class CountWords {


public static int countWords(String str) {
String[] words = str.split(" ");
return words.length;
}

public static void main(String[] args) {


String input = "Capgemini is a global company";
int output = countWords(input);
System.out.println("Number of Words: " + output);
}
}

Program 19: Calculate Average of Array Elements

Pseudo Code Interview Questions With Answers 19


Question: Write a program to calculate the average of elements in an array.
Input: [10, 20, 30, 40, 50]

Output: 30.0

Explanation: Compute the average by summing the elements and dividing by


the number of elements.

Pseudo Code:

1. Take input array arr .

2. Initialize sum to 0.

3. For each element num in arr , add num to sum .

4. Return sum divided by the length of arr .

public class AverageOfArray {


public static double calculateAverage(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return (double) sum / arr.length;
}

public static void main(String[] args) {


int[] arr = {10, 20, 30, 40, 50};
double output = calculateAverage(arr);
System.out.println("Average: " + output);
}
}

Program 20: Find Missing Number in Array


Question: Write a program to find the missing number in a given array of 1 to n.
Input: [1, 2, 4, 5, 6]

Output: 3

Explanation: Identify the missing number in the array where the numbers range
from 1 to n.

Pseudo Code Interview Questions With Answers 20


Pseudo Code:

1. Take input array arr and integer n .

2. Calculate sum1 as the sum of numbers from 1 to n using the formula n * (n

+ 1) / 2 .

3. Calculate sum2 as the sum of elements in arr .

4. Return sum1 - sum2 .

public class MissingNumber {


public static int findMissingNumber(int[] arr, int n) {
int sum1 = n * (n + 1) / 2;
int sum2 = 0;
for (int num : arr) {
sum2 += num;
}
return sum1 - sum2;
}

public static void main(String[] args) {


int[] arr = {1, 2, 4, 5, 6};
int n = 6;
int output = findMissingNumber(arr, n);
System.out.println("Missing Number: " + output);
}
}

You can download Java technical interview programming questions that were
asked in Tech Mahindra, TCS, Wipro, Infosys, Accenture, Capgemini, and many
other service and product-based companies.

Pseudo Code Interview Questions With Answers 21

You might also like