lOMoARcPSD|58912199
Capgemini Coding questions
Question 1
Problem Statement –
You have write a function that accepts, a string which length is “len”, the string has some “#”, in
it you have to move all the hashes to the front of the string and return the whole string back and
print it.
char* moveHash(char str[],int n);
example :-
Sample Test Case
Input:
Move#Hash#to#Front
Output:
###MoveHashtoFront
Question 2 : Sum of K Farthest items
Problem Statement :
You are given an array of length “len” ,another item called k and an integer value x. Your job is to
find the sum of k farthest items in the array from x.
First line has len, k and x respectively
2nd line has the array
Example :
Input :
5 3 20
21 4 15 17 11
Output :
30
4, 15 and 11 are farthest from 20. Thus, their sum will be the answer.
3. Problem Statement:
You have write a function that accepts, a string which length is “lenˮ, the string has some “#ˮ, in
it you have to move all the hashes to the front of the s
Question 3 : Find Nth largest and smallest
Problem Statement :
Write a program to find the nth largest and nth smallest item in the array and print them in the
same line.
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Input Format
The first line has the following –
Size of array, value of n
2nd line has the array
Output Format
Nth largest and nth smallest respectively
Input : Output :
63 34
123456
1.Problem Statement:
Capgemini in its online written test have a coding question, where in the students are given a
string with multiple characters that are repeated consecutively. You’re supposed to reduce
the size of this string using mathematical logic given as in the example below:
Input:
aabbbbeeeeffggg
Output:
a2b4e4f2g3
1. Problem Statement:
Write the code to traverse a matrix in a spiral format.
Sample Input
Input
5 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Output
1 2 3 4 8 12 16 20 19 18 17 13 9 5 6 7 11 15 12 14 10
2. Problem Statement:
You are given an array of integers, print the number of times each integer has occurred in
the array.
Input:
10
1 233414512
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Output:
1 occurs 3 times
2 occurs 2 times
3 occurs 2 times
4 occurs 2 times
5 occurs 1 times
3. Problem Statement:
Write a function to solve the following equation a3 + a2b + 2a2b + 2ab2 + ab2 + b3. Write a
program to accept three values in order of a, b and c and get the result of the above
equation.
4. Problem Statement:
A function is there which tells how many dealerships there are and the total number of cars in
each dealership. Your job is to calculate how many tyres would be there in each dealership.
Input
42
40
1 2
Output
20
16
There are total 3 dealerships dealerships1 contains
4 cars and 2 bikes dealerships2 contains 4 cars and
0 bikes dealerships3 contains 1 cars and 2 bikes
Total number of tyres in dealerships1 is (4 x 4) + (2 x 2) = 20
Total number of tyres in dealerships2 is (4 x 4) + (0 x 2) = 16
Total number of tyres in dealerships3 is (1 x 4) + (2 x 2) = 8
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
6. Problem Statement –
You have write a function that accepts, a string which length is “len”, the string has some “#”,
in it you have to move all the hashes to the front of the string and return the whole string
back and print it.
char* moveHash(char str[],int n);
example :Sample
Test Case
Input:
Move#Hash#to#Front
Output:
###MoveHashtoFront
7. Problem Statement:
Mayuri buys “N” no of products from a shop. The shop offers a different percentage of
discount on each item. She wants to know the item that has the minimum discount offer, so
that she can avoid buying that and save money.
[Input Format: The first input refers to the no of items; the second input is the item name,
price and discount percentage separated by comma(,)]
Assume the minimum discount offer is in the form of Integer.
Note: There can be more than one product with a minimum discount.
Sample Input 1:
4
mobile,10000,20
shoe,5000,10
watch,6000,15
laptop,35000,5
Sample Output 1:
shoe
Explanation: The discount on the mobile is 2000, the discount on the shoe is 500, the
discount on the watch is 900 and the discount on the laptop is 1750. So the discount on the
shoe is the minimum.
Sample Input 2:
4
Mobile,5000,10
shoe,5000,10
WATCH,5000,10
Laptop,5000,10
Sample Output 2:
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Mobile shoe
WATCH
Laptop
8. Problem Statement:
Bela teaches her daughter to find the factors of a given number. When she provides a
number to her daughter, she should tell the factors of that number. Help her to do this, by
writing a program. Write a class FindFactor.java and write the main method in it.
Note :
If the input provided is negative, ignore the sign and provide the output. If the input is zero
If the input is zero the output should be “No Factors”.
Sample Input 1:
54
Sample Output 1:
1, 2, 3, 6, 9, 18, 27, 54
Sample Input 2:
-1869
Sample Output 2 :
1, 3, 7, 21, 89, 267, 623, 1869
9. Problem Statement:
Raj wants to know the maximum marks scored by him in each semester. The mark should be
between 0 to 100 ,if it goes beyond the range display “You have entered invalid mark.”
Sample Input 1:
Enter no of semester:
3
Enter no of subjects in 1 semester:
3
Enter no of subjects in 2 semester:
4
Enter no of subjects in 3 semester:
2
Marks obtained in semester 1:
50
60
70
Marks obtained in semester 2:
90
98
76
67
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Marks obtained in semester 3:
89
76
Sample Output 1:
Maximum mark in 1 semester:70
Maximum mark in 2 semester:98
Maximum mark in 3 semester:89
Sample Input 2:
Enter no of semester:
3
Enter no of subjects in 1 semester:
3
Enter no of subjects in 2 semester:
4
Enter no of subjects in 3 semester:
2
Marks obtained in semester 1:
55
67
98
Marks obtained in semester 2:
67
(-)98
Sample Output 2:
You have entered invalid mark.
10. Problem Statement:
IIHM institution is offering a variety of courses to students. Students have a facility to check
whether a particular course is available in the institution. Write a program to help the
institution accomplish this task. If the number is less than or equal to zero display “Invalid
Range”.
Assume the maximum number of courses is 20.
Sample Input 1:
Enter no of course:
5
Enter course names:
Java
Oracle
C++
Mysql
Dotnet
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Enter the course to be searched:
C++
Sample Output 1: C++
course is available
Sample Input 2:
Enter no of course:
3
Enter course names:
Java
Oracle
Dotnet
Enter the course to be searched:
C++
Sample Output 2: C++
course is not available
Sample Input 3:
Enter no of course:
0
Sample Output 3:
Invalid Range
11. Problem Statement:
Shraddha Kapoor’s professor suggested that she study hard and prepare well for the lesson
on seasons. If her professor says month then, she has to tell the name of the season
corresponding to that month. So write the program to get the solution to the above task?
March to May – Spring Season
June to August – Summer Season
September to November – Autumn Season
December to February – Winter Season
Note: The entered month should be in the range of 1 to 12. If the user enters a month less
than 1 or greater than 12 then the message “Invalid Month Entered” should get
displayed. Sample Input 1: Enter month: 6 Sample Output 1: Season: Summer Sample
Input 2:
Enter month: 15
Sample Output 2:
Invalid Month Entered
12. String Rotation:
Problem: Determine if one string is a rotation of another.
Input:
String A: “ABCD”
String B: “CDAB”
Output: True.
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
Downloaded by Ankit (ankitsmile9926@gmail.com)
lOMoARcPSD|58912199
13. Find the Intersection of two arrays
14. Check for palindrome
15. Matrix Rotation by 90 degrees
16. Find the Majority element
17. Find the first non – repeated character in a string
18. Merge intervals
19. Largest sum contiguous subarray (kadane’s algorithm)
20. Check for balanced parentheses
21. Rotate an array
22. Merge two sorted arrays
23. Find the missing number in an array
24. Reverse a linked list
25. Longest SubString without repeating characters
26. Find the duplicates in an array
Downloaded by Ankit (ankitsmile9926@gmail.com)