Capgemini pseudo code questions with answer for freshers. In the competitive world of job interviews, especially in tech roles at leading companies like Capgemini, demonstrating your programming skills is crucial. To help you prepare effectively, we’ve compiled a list of 20 essential Java programs that are frequently asked in interviews. Each program includes a question, input and output examples, an explanation, and the Java code to solve it.
Download Capgemini pseudo code questions with answers
What is flow chart and pseudo code ?
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:
- Take input string
str
. - Initialize an empty string
reversed
. - For each character
ch
instr
from the end to the beginning, appendch
toreversed
. - 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);
}
}
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:
- Take input string
str
. - Initialize two pointers
left
at 0 andright
atstr.length - 1
. - While
left
is less thanright
, do:
- If
str.charAt(left)
is not equal tostr.charAt(right)
, return false. - Increment
left
and decrementright
.
- 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);
}
}
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:
- Take input integer
n
. - Initialize
a
to 0 andb
to 1. - Print
a
andb
. - For
i
from 2 ton-1
, do:
- Set
c
toa + b
. - Print
c
. - Set
a
tob
andb
toc
.
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);
}
}
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:
- Take input integer
n
. - Initialize
result
to 1. - For
i
from 1 ton
, do:
- Multiply
result
byi
.
- 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
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:
- Take input integer
n
. - If
n
is less than or equal to 1, return false. - For
i
from 2 to the square root ofn
, do:
- If
n % i
is 0, return false.
- 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:
- Take input integers
a
andb
. - While
b
is not 0, do:
- Set
temp
tob
. - Set
b
toa % b
. - Set
a
totemp
.
- 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:
- Take input string
str
. - Initialize
count
to 0. - For each character
ch
instr
, do:
- If
ch
is a vowel, incrementcount
.
- 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:
- Take input string
str
. - Initialize
result
as an empty string. - For each character
ch
instr
, do:
- If
ch
is not inresult
, addch
toresult
.
- 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:
- Take input array
arr
. - Initialize
max
toarr[0]
. - For each element
num
inarr
, do:
- If
num
is greater thanmax
, setmax
tonum
.
- 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:
- Take input array
arr
. - Initialize
min
toarr[0]
. - For each element
num
inarr
, do:
- If
num
is less thanmin
, setmin
tonum
.
- Return
min
.
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:
- Take input strings
str1
andstr2
. - If lengths of
str1
andstr2
are not equal, return false. - Sort both strings.
- If sorted strings are equal, return true. Otherwise, return false.
import java.util.Arrays;
public class AnagramCheck {
public static boolean areAnagrams(String str1, String str2) {
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:
- Take input arrays
arr1
andarr2
. - Initialize an empty array
result
. - Initialize pointers
i
andj
to 0. - While
i
is less than the length ofarr1
andj
is less than the length ofarr2
, do:
- If
arr1[i]
is less thanarr2[j]
, addarr1[i]
toresult
and incrementi
. - Else, add
arr2[j]
toresult
and incrementj
.
- Add remaining elements of
arr1
andarr2
toresult
. - 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.toString(output));
}
}
Program 13: Binary Search
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:
- Take input array
arr
and integerkey
. - Initialize
left
to 0 andright
toarr.length - 1
. - While
left
is less than or equal toright
, do:
- Set
mid
to(left + right) / 2
. - If
arr[mid]
is equal tokey
, returnmid
. - If
arr[mid]
is less thankey
, setleft
tomid + 1
. - Else, set
right
tomid - 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;
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:
- Take input array
arr
. - Initialize
n
toarr.length
. - For
i
from 0 ton-1
, do:
- For
j
from 0 ton-i-1
, do:- If
arr[j]
is greater thanarr[j+1]
, swap them.
- If
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);
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
toarr[i]
. - Set
j
toi - 1
. - While
j
>= 0 andarr[j]
>key
, do:- Set
arr[j + 1]
toarr[j]
. - Decrement
j
.
- Set
- Set
arr[j + 1]
tokey
.
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;
}
}
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:
- Take input array
arr
. - If
arr.length
<= 1, returnarr
. - Split
arr
intoleft
andright
halves. - Recursively sort
left
andright
. - Merge sorted
left
andright
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.length);
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]
Explanation: Sort the array using the quick sort algorithm.
Pseudo Code:
- Take input array
arr
, and integerslow
andhigh
. - If
low
<high
, do:
- Set
pi
to partitionarr
withlow
andhigh
. - Recursively quick sort
arr[low..pi-1]
andarr[pi+1..high]
.
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
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 high) {
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) {
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:
- Take input string
str
. - Split
str
by spaces into an arraywords
. - 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
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:
- Take input array
arr
. - Initialize
sum
to 0. - For each element
num
inarr
, addnum
tosum
. - Return
sum
divided by the length ofarr
.
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:
- Take input array
arr
and integern
. - Calculate
sum1
as the sum of numbers from 1 ton
using the formulan * (n + 1) / 2
. - Calculate
sum2
as the sum of elements inarr
. - 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.