Capgemini Coding CheatSheet
Capgemini Coding CheatSheet
1. Problem Statement –
Capgemini in its online written test have a coding question, wherein 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 :
G
Input :
aabbbbeeeeffggg
IN
Output:
a2b4e4f2g3
D
O
2. Problem Statement –
Input
E
5 4
IM
1 2 3 4
5 6 7 8
9 10 11 12
PR
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
3. Problem Statement –
You’re given an array of integers, print the number of times each integer has
occurred in the array.
Example
Input :
10
1233414512
Output :
1 occurs 3 times
G
2 occurs 2 times
3 occurs 2 times
IN
4 occurs 2 times
5 occurs 1 times
D
O
4. Problem Statement –
C
Write a function to solve the following equation a3 + a2b + 2a2b + 2ab2 +
ab2 + b3.
E
Write a program to accept three values in order of a, b and c and get the result
of the above equation.
IM
PR
5. 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
3
42
40
12
Output
20
16
G
Total number of tyres in dealerships1 is (4 x 4) + (2 x 2) = 20
IN
Total number of tyres in dealerships2 is (4 x 4) + (0 x 2) = 16
D
O
6. Problem Statement
Bela teaches her daughter to find the factors of a given number. When she
C
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.
E
Note :
IM
If the input provided is negative, ignore the sign and provide the output. If
the input is zero
PR
Sample Input 1:
54
Sample Output 1:
1, 2, 3, 6, 9, 18, 27, 54
7. 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
Sample Input 1:
Enter no of semester:3
G
Marks obtained in semester 2:90987667
IN
Sample Output 1:
Mayuri buys “N” no of products from a shop. The shop offers a different
E
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
IM
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:
PR
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.
9. 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.
G
char* moveHash(char str[],int n);
IN
example :-
Move#Hash#to#Front D
O
Output:
MoveHashtoFront
C
E
10. Shraddha Kapoor’s professor suggested that she study hard and prepare
IM
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?
PR
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
Input:
G
8
IN
UDDDUDUU
Output: 1
D
Explanation: A valley is a sequence of consecutive steps below sea level.
The example describes a single valley.
O
C
12. Matrix Identity Check:
Input:
IM
Problem: Generate all Pythagorean triplets with values smaller than a given
limit.
Input: limit = 20
Output:
3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
G
IN
14. Binary Search:
Input: D
O
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
C
Target: 4
Output: 3
Explanation: The function returns the index of the target value in the
E
array.
IM
Input:
String A: "ABCD"
String B: "CDAB"
Output: True
Explanation: B is a rotation of A .
Input:
Array: [1, 2, 4, 5, 6]
Output: 3
G
17. Reverse a Linked List:
IN
Problem: Write a function to reverse a singly linked list.
Input:
Output:
1 -> 2 -> 3 -> 4 -> 5
D
O
5 -> 4 -> 3 -> 2 -> 1
C
Explanation: The list is reversed.
Problem: Given a string, find the length of the longest substring without
repeating characters.
IM
Input:
String: "abcabcbb"
PR
Output: 3
Input:
Array: [4, 3, 2, 7, 8, 2, 3, 1]
Output:
Duplicates: 2, 3
Input:
Number: 29
Output: True
G
21. Merge Two Sorted Arrays:
IN
Input:
Array 1: [1, 3, 5]
Output:
Array 2: [2, 4, 6]
D
O
[1, 2, 3, 4, 5, 6]
C
22. Rotate an Array:
Input:
IM
Array: [1, 2, 3, 4, 5]
k = 2
Output:
PR
[4, 5, 1, 2, 3]
Input:
String: "{[()]}"
Output:
True
Input:
Output:
G
6
IN
25. Find the Intersection of Two Arrays:
Input:
D
O
Array 1: [1, 2, 2, 1]
Array 2: [2, 2]
C
Output:
[2]
E
Input:
String: "racecar"
Output: True
Output:
Problem: Given an array of size n , find the majority element (appears more
than n/2 times).
G
Input:
IN
Array: [2, 2, 1, 1, 1, 2, 2]
Output:
Problem: Given a string, find the first character that does not repeat.
Input:
E
String: "swiss"
IM
Output:
w
PR
Explanation: 'w' is the first character that does not repeat in the string.
Input:
Intervals: [[1,3],[2,6],[8,10],[15,18]]
Output:
[[1,6],[8,10],[15,18]]
Explanation: Intervals [1,3] and [2,6] overlap, so they are merged into
[1,6]. The others remain unchanged.
G
IN
D
O
C
E
IM
PR