Capgemini coding questions (2026): practice problems with solutions
Eight practice problems in the style of the coding questions students report from Capgemini's fresher hiring: number theory basics, strings, sorting, prefix sums, a simple dynamic programming problem and a matrix. Each one has the idea in plain words, a tested solution in Python, C++ and Java, and its complexity.
We wrote every problem ourselves to match the common question types, so these aren't official Capgemini questions. Use them to learn the patterns, then take the real test on your own.
The problems
- How many ways to arrange the chairs?Maths · Divisors · Easy
- The remainder of a huge powerMaths · Fast power · Medium
- Add up the numbers hidden in a textStrings · Easy
- Print the merit listSorting · Easy
- The two closest numbersSorting · Easy
- Total sales between two daysPrefix sums · Arrays · Medium
- Ways to pay with coinsDynamic programming · Medium
- Multiply two matricesMatrices · Easy
What Capgemini's own pages say
Exceller is Capgemini's programme for technical graduates, and Capgemini says to reach it through your college's placement team. When we checked, no 2026 or 2027 engineering drive was published, so here is its current recruitment process and the most recent engineering drive it described in detail.
| Step | Recruitment process now | 2022 engineering and MCA drive |
|---|---|---|
| 1 | Online aptitude test: logical reasoning, verbal ability and quantitative skills | Technical assessment (pseudocode) |
| 2 | Coding or technical assessment | English communication test (multiple choice) |
| 3 | Group discussion, for some roles | Game-based aptitude test |
| 4 | Technical interview | Behavioural competency profiling |
| 5 | HR interview | Technical and HR interview |
- Capgemini says its online technical and language tests are proctored, where they apply.
- The 2022 drive's rules: MCA, or a B.E. or B.Tech in any branch; at least 60% in diploma and graduation; no more than 1 year of gap between stages; no backlog when you take the test; and a service agreement.
- Its recent drives also ask for Aadhaar verification and an active DigiLocker account.
- Not published: pay, the number of questions, timings, negative marking and coding languages. We haven't guessed.
Sources: Capgemini's technical graduates page, its recruitment process and its 2022 engineering and MCA drive. Checked on 17 October 2026.
How to practise with this page
- Look at the limits before you choose an approach. With 100,000 numbers, a loop inside a loop is about 10 billion steps, far too slow, so aim for one pass or a sort.
- When many questions ask about ranges of the same list, build prefix sums once and answer each question with one subtraction.
- Number problems often have a faster route than counting one by one: divisors come in pairs, powers can be built by repeated squaring, and many sums have a formula.
- When the answer is asked modulo a number, take the remainder at every step so the values never overflow.
- If you get stuck, write the slow but simple answer first, then improve it. Keep the simple version: running both on small inputs and comparing the answers is a quick way to catch bugs.
- Capgemini's last detailed engineering drive started with pseudocode, so practise reading code and predicting its output, not only writing it.
1How many ways to arrange the chairs?
A hall has N chairs, and they must be set out in equal rows with no chair left over. For example, 12 chairs can be 1 row of 12, 2 rows of 6, 3 rows of 4, 4 rows of 3, 6 rows of 2 or 12 rows of 1. In how many ways can the N chairs be set out?
- Input
- N (1 ≤ N ≤ 1012).
- Output
- The number of ways.
Example input
12
Output
6
The six ways are listed above: one for each number that divides 12 exactly (1, 2, 3, 4, 6 and 12).
Approach
Each way is a number of rows that divides N exactly, so the answer is the number of divisors of N. Trying every number from 1 to N would take 1012 steps. But divisors come in pairs: if d divides N, so does N ÷ d, and one of the two is at most √N.
So try only the d with d × d ≤ N, at most 106 of them. Each d that divides N counts twice (d and N ÷ d), except when d × d = N, which counts once. N goes up to 1012, so use 64-bit integers, including for d × d.
Solution
Python
import sys
n = int(sys.stdin.read())
ways = 0
d = 1
while d * d <= n: # divisors come in pairs: d and n // d
if n % d == 0:
ways += 1 if d * d == n else 2 # a square root pairs with itself
d += 1
print(ways)C++
#include <iostream>
using namespace std;
int main() {
long long n;
cin >> n;
long long ways = 0;
for (long long d = 1; d * d <= n; d++) // divisors come in pairs: d and n / d
if (n % d == 0)
ways += (d * d == n) ? 1 : 2; // a square root pairs with itself
cout << ways << '\n';
}Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long ways = 0;
for (long d = 1; d * d <= n; d++) // divisors come in pairs: d and n / d
if (n % d == 0)
ways += (d * d == n) ? 1 : 2; // a square root pairs with itself
System.out.println(ways);
}
}Time: O(√N) · Extra space: O(1)
2The remainder of a huge power
Print AB mod M: the remainder when A multiplied by itself B times is divided by M. Any number to the power 0 is 1.
- Input
- A, B and M (1 ≤ A ≤ 1018, 0 ≤ B ≤ 1018, 1 ≤ M ≤ 109).
- Output
- AB mod M.
Example 1 input
2 10 1000
Output
24
Example 2 input
3 200 13
Output
9
210 = 1024, which leaves 24 when divided by 1000. In the second example, 33 = 27 leaves 1 when divided by 13, so 3200 = (33)66 × 32 leaves 1 × 9 = 9.
Approach
Multiplying B times is far too slow when B is 1018, and the power itself would be enormous. Two ideas fix this. First, you can take the remainder after every multiplication without changing the final remainder, so the numbers stay below M. Second, AB can be built from A, A2, A4, A8 and so on, using the ones that match the 1s of B written in binary.
As a loop: if B is odd, multiply the result by the current A; then square A and halve B (dropping the remainder). That takes about 60 rounds for B up to 1018. Reduce A mod M first, so every product stays below 1018 and fits in a 64-bit integer, and start the result at 1 mod M so that M = 1 gives 0. Python's built-in pow(a, b, m) does all of this for you.
Solution
Python
import sys
a, b, m = map(int, sys.stdin.read().split())
result = 1 % m # A to the power 0 is 1 (and 1 mod 1 is 0)
a %= m
while b > 0:
if b % 2 == 1: # this bit of B is set, so multiply this power in
result = result * a % m
a = a * a % m # A, A^2, A^4, A^8, ... (all mod M)
b //= 2
print(result) # Python's built-in pow(a, b, m) gives the same answerC++
#include <iostream>
using namespace std;
int main() {
long long a, b, m;
cin >> a >> b >> m;
long long result = 1 % m; // A to the power 0 is 1 (and 1 mod 1 is 0)
a %= m; // now a < 10^9, so a * a stays below 10^18
while (b > 0) {
if (b % 2 == 1) // this bit of B is set, so multiply this power in
result = result * a % m;
a = a * a % m; // A, A^2, A^4, A^8, ... (all mod M)
b /= 2;
}
cout << result << '\n';
}Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong(), b = sc.nextLong(), m = sc.nextLong();
long result = 1 % m; // A to the power 0 is 1 (and 1 mod 1 is 0)
a %= m; // now a < 10^9, so a * a stays below 10^18
while (b > 0) {
if (b % 2 == 1) // this bit of B is set, so multiply this power in
result = result * a % m;
a = a * a % m; // A, A^2, A^4, A^8, ... (all mod M)
b /= 2;
}
System.out.println(result);
}
}Time: O(log B) · Extra space: O(1)
4Print the merit list
N students each have a name and marks. Print the merit list: students with higher marks come first, and students with the same marks are listed in alphabetical order of their names.
- Input
- N (1 ≤ N ≤ 105), then N lines, each with a name (1 to 20 small letters) and marks (0 to 100). No two students have the same name.
- Output
- The names in merit-list order, one per line.
Example input
5 riya 82 aman 91 karan 82 zoya 91 neha 75
Output
aman zoya karan riya neha
aman and zoya have 91, so they come first, in alphabetical order; then karan and riya with 82, then neha.
Approach
Sort with a rule that compares two students: the one with more marks goes first, and if their marks are equal, the name that comes first in the alphabet goes first. In Python, sorting by the pair (−marks, name) does exactly this; in C++ and Java, write the rule as a comparison function.
Every language's built-in sort accepts a rule like this, so there's no need to write your own sorting loop. A hand-written bubble sort would take O(N²) steps, too slow for 105 students.
Solution
Python
import sys
data = sys.stdin.read().split()
n = int(data[0])
students = [(data[1 + 2 * i], int(data[2 + 2 * i])) for i in range(n)]
students.sort(key=lambda s: (-s[1], s[0])) # higher marks first, then names from A to Z
print("\n".join(name for name, _ in students))C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<string, int>> s(n); // (name, marks)
for (auto &[name, marks] : s) cin >> name >> marks;
sort(s.begin(), s.end(), [](const pair<string, int> &x, const pair<string, int> &y) {
if (x.second != y.second) return x.second > y.second; // higher marks first
return x.first < y.first; // then names from A to Z
});
for (auto &[name, marks] : s) cout << name << '\n';
}Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] name = new String[n];
int[] marks = new int[n];
Integer[] order = new Integer[n]; // student numbers, to be sorted
for (int i = 0; i < n; i++) {
name[i] = sc.next();
marks[i] = sc.nextInt();
order[i] = i;
}
Arrays.sort(order, (x, y) -> marks[x] != marks[y]
? Integer.compare(marks[y], marks[x]) // higher marks first
: name[x].compareTo(name[y])); // then names from A to Z
StringBuilder out = new StringBuilder();
for (int i : order) out.append(name[i]).append('\n');
System.out.print(out);
}
}Time: O(N log N) · Extra space: O(N)
5The two closest numbers
Given N numbers, find the smallest difference between any two of them (the bigger one minus the smaller one). The same number may appear more than once.
- Input
- N (2 ≤ N ≤ 105), then N numbers, each between -109 and 109.
- Output
- The smallest difference.
Example input
5 30 5 20 9 14
Output
4
In sorted order the numbers are 5 9 14 20 30, and the closest two are 5 and 9.
Approach
Comparing every pair takes O(N²) steps. Sort the numbers first, and the closest pair is always two neighbours: if two numbers aren't next to each other, the number between them is at least as close to each of them. So one pass over the neighbours finds the answer.
If a number appears twice, the answer is 0, and sorting puts the two copies side by side, so this needs no special case. A difference can be up to 2 × 109, close to the limit of a 32-bit int, so the C++ and Java versions use 64-bit numbers.
Solution
Python
import sys
data = list(map(int, sys.stdin.read().split()))
n = data[0]
nums = sorted(data[1:1 + n])
# After sorting, the closest two numbers are always next to each other.
print(min(nums[i + 1] - nums[i] for i in range(n - 1)))C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n); // a gap can be up to 2 x 10^9, right at the edge of int
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
// After sorting, the closest two numbers are always next to each other.
long long best = a[1] - a[0];
for (int i = 1; i + 1 < n; i++) best = min(best, a[i + 1] - a[i]);
cout << best << '\n';
}Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n]; // a gap can be up to 2 x 10^9, right at the edge of int
for (int i = 0; i < n; i++) a[i] = sc.nextLong();
Arrays.sort(a);
// After sorting, the closest two numbers are always next to each other.
long best = a[1] - a[0];
for (int i = 1; i + 1 < n; i++) best = Math.min(best, a[i + 1] - a[i]);
System.out.println(best);
}
}Time: O(N log N) for the sort · Extra space: O(N)
6Total sales between two days
A shop wrote down its sales for each of N days, numbered 1 to N. The owner then asks Q questions. Each question gives two days, L and R, and asks for the total sales from day L to day R, including both. Answer every question.
- Input
- N and Q (1 ≤ N, Q ≤ 105), then the N daily sales (each from 0 to 109), then Q lines, each with L and R (1 ≤ L ≤ R ≤ N).
- Output
- Q lines: the answer to each question, in order. An answer can be up to 1014, so use 64-bit integers.
Example input
6 3 4 2 7 1 3 5 1 3 2 6 4 4
Output
13 18 1
Days 1 to 3: 4 + 2 + 7 = 13. Days 2 to 6: 2 + 7 + 1 + 3 + 5 = 18. Day 4 alone: 1.
Approach
Adding up each range from scratch can take 105 steps per question, so 1010 in all: far too slow. Instead, build prefix sums once: prefix[i] is the total of the first i days, with prefix[0] = 0 and prefix[i] = prefix[i − 1] + sales[i].
The total from day L to day R is everything up to day R minus everything before day L: prefix[R] − prefix[L − 1]. Each question now takes one subtraction. Starting with prefix[0] = 0 is what makes L = 1 work without a special case.
Solution
Python
import sys
data = list(map(int, sys.stdin.read().split()))
n, q = data[0], data[1]
sales = data[2:2 + n]
prefix = [0] * (n + 1) # prefix[i] = total of the first i days
for i in range(n):
prefix[i + 1] = prefix[i] + sales[i]
answers = []
pos = 2 + n
for _ in range(q):
left, right = data[pos], data[pos + 1]
pos += 2
answers.append(prefix[right] - prefix[left - 1]) # days left to right
print("\n".join(map(str, answers)))C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector<long long> prefix(n + 1, 0); // prefix[i] = total of the first i days
for (int i = 1; i <= n; i++) {
long long x;
cin >> x;
prefix[i] = prefix[i - 1] + x;
}
vector<long long> answers(q);
for (auto &ans : answers) {
int left, right;
cin >> left >> right;
ans = prefix[right] - prefix[left - 1]; // days left to right
}
for (long long ans : answers) cout << ans << '\n';
}Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), q = sc.nextInt();
long[] prefix = new long[n + 1]; // prefix[i] = total of the first i days
for (int i = 1; i <= n; i++) prefix[i] = prefix[i - 1] + sc.nextLong();
StringBuilder out = new StringBuilder();
for (int k = 0; k < q; k++) {
int left = sc.nextInt(), right = sc.nextInt();
out.append(prefix[right] - prefix[left - 1]).append('\n'); // days left to right
}
System.out.print(out);
}
}Time: O(N + Q) · Extra space: O(N)
7Ways to pay with coins
You have coins of M different values, and as many of each value as you like. In how many different ways can you pay exactly N rupees? Two ways count as the same if they use the same number of each coin, in any order. The count can be huge, so print it modulo 1,000,000,007.
- Input
- N and M (1 ≤ N ≤ 104, 1 ≤ M ≤ 100), then M different coin values, each from 1 to 104.
- Output
- The number of ways, modulo 1,000,000,007 (0 if N can't be paid).
Example 1 input
5 3 1 2 5
Output
4
Example 2 input
3 1 2
Output
0
5 can be paid as 5, 2 + 2 + 1, 2 + 1 + 1 + 1 or 1 + 1 + 1 + 1 + 1. In the second example, coins of 2 can only make even amounts.
Approach
Let ways[x] be the number of ways to pay x. Start with ways[0] = 1 (paying nothing, with no coins) and everything else at 0. Then bring in the coins one value at a time: for a coin c, every way to pay x − c becomes a way to pay x by adding one more c, so for x from c up to N, add ways[x − c] to ways[x].
The order of the loops matters. With the coins on the outside, each combination is built in one fixed order of coin values, so 2 + 1 and 1 + 2 count once. With the amounts on the outside, you would count every order separately, so 2 + 1 and 1 + 2 would be two ways. Going upwards through x lets the same coin be used again and again. Take the modulo at every step so the numbers stay small.
Solution
Python
import sys
MOD = 1_000_000_007
data = list(map(int, sys.stdin.read().split()))
n, m, coins = data[0], data[1], data[2:]
ways = [0] * (n + 1) # ways[x] = ways to pay x with the coins used so far
ways[0] = 1 # one way to pay 0: use no coins
for c in coins[:m]: # one coin at a time, so the order of coins never matters
for x in range(c, n + 1):
ways[x] = (ways[x] + ways[x - c]) % MOD
print(ways[n])C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
const long long MOD = 1000000007;
int n, m;
cin >> n >> m;
vector<long long> ways(n + 1, 0); // ways[x] = ways to pay x with the coins used so far
ways[0] = 1; // one way to pay 0: use no coins
for (int i = 0; i < m; i++) { // one coin at a time, so the order of coins never matters
int c;
cin >> c;
for (int x = c; x <= n; x++)
ways[x] = (ways[x] + ways[x - c]) % MOD;
}
cout << ways[n] << '\n';
}Java
import java.util.*;
public class Main {
public static void main(String[] args) {
final long MOD = 1_000_000_007L;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
long[] ways = new long[n + 1]; // ways[x] = ways to pay x with the coins used so far
ways[0] = 1; // one way to pay 0: use no coins
for (int i = 0; i < m; i++) { // one coin at a time, so the order of coins never matters
int c = sc.nextInt();
for (int x = c; x <= n; x++)
ways[x] = (ways[x] + ways[x - c]) % MOD;
}
System.out.println(ways[n]);
}
}Time: O(N × M) · Extra space: O(N)
8Multiply two matrices
Multiply matrix A, which has N rows and M columns, by matrix B, which has M rows and P columns. The answer C has N rows and P columns. To find the number in row i and column j of C, go along row i of A and down column j of B at the same time, multiply each pair of numbers, and add up the results: C[i][j] = A[i][1] × B[1][j] + A[i][2] × B[2][j] + … + A[i][M] × B[M][j].
- Input
- N, M and P (1 ≤ N, M, P ≤ 100), then A as N lines of M numbers, then B as M lines of P numbers. Every number is between -1,000 and 1,000.
- Output
- C as N lines of P numbers, separated by spaces.
Example input
2 3 2 1 2 3 4 5 6 7 8 9 10 11 12
Output
58 64 139 154
The top-left number is row 1 of A times column 1 of B: 1 × 7 + 2 × 9 + 3 × 11 = 58.
Approach
Use three loops: i over the rows of A, j over the columns of B, and k along the shared length M, adding A[i][k] × B[k][j] into the sum for C[i][j]. That is N × M × P multiplications, at most 106 here.
Two checks before you code: the number of columns of A must equal the number of rows of B (both are M here), and the answer has A's rows and B's columns. Each number in C is at most 100 × 1,000 × 1,000 = 108 in size, so an ordinary int is enough this time.
Solution
Python
import sys
data = list(map(int, sys.stdin.read().split()))
n, m, p = data[0], data[1], data[2]
pos = 3
a = [data[pos + i * m: pos + (i + 1) * m] for i in range(n)] # n rows of m
pos += n * m
b = [data[pos + k * p: pos + (k + 1) * p] for k in range(m)] # m rows of p
for i in range(n):
# C[i][j] = row i of A matched number by number with column j of B
print(*(sum(a[i][k] * b[k][j] for k in range(m)) for j in range(p)))C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<vector<int>> a(n, vector<int>(m)), b(m, vector<int>(p));
for (auto &row : a)
for (int &x : row) cin >> x;
for (auto &row : b)
for (int &x : row) cin >> x;
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
int sum = 0; // at most 100 x 1000 x 1000 = 10^8, so int is enough
for (int k = 0; k < m; k++)
sum += a[i][k] * b[k][j]; // row i of A, column j of B
cout << sum << (j + 1 < p ? ' ' : '\n');
}
}
}Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt(), p = sc.nextInt();
int[][] a = new int[n][m], b = new int[m][p];
for (int i = 0; i < n; i++)
for (int k = 0; k < m; k++) a[i][k] = sc.nextInt();
for (int k = 0; k < m; k++)
for (int j = 0; j < p; j++) b[k][j] = sc.nextInt();
StringBuilder out = new StringBuilder();
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
int sum = 0; // at most 100 x 1000 x 1000 = 10^8, so int is enough
for (int k = 0; k < m; k++)
sum += a[i][k] * b[k][j]; // row i of A, column j of B
out.append(sum).append(j + 1 < p ? " " : "\n");
}
}
System.out.print(out);
}
}Time: O(N × M × P) · Extra space: O(N × M + M × P) for the two matrices
Questions
What is Capgemini's recruitment process for freshers?
Its recruitment page lists an online aptitude test (logical reasoning, verbal ability and quantitative skills), a coding or technical assessment, a group discussion for some roles, then technical and HR interviews.
Does Capgemini have a pseudocode test?
Its 2022 engineering and MCA drive started with a pseudocode technical assessment, followed by an English test, a game-based aptitude test and behavioural profiling. Capgemini hasn't said whether later drives use the same steps.
Who can apply to Capgemini Exceller?
Capgemini says to get in touch with your college placement team. Its 2022 drive asked for at least 60% in diploma and graduation, no backlog and at most 1 year of gap between stages.
Are these real Capgemini questions?
No. We wrote them ourselves in the style of the question types students commonly report: number theory, strings, sorting, prefix sums, a simple DP and a matrix.
Were these solutions tested?
Yes. We ran every solution in Python, C++ and Java on the examples, on extra edge cases, on random inputs checked against a slow but simple answer, and on the largest inputs the limits allow.
More placement prep
- TCS NQT coding questions: 10 practice problems with tested Python, C++ and Java solutions, and the test pattern from TCS's own page.
- Infosys and Accenture coding questions: 10 practice problems from strings to greedy and dynamic programming, with each test's format from the companies' own pages.
- Wipro coding questions: 8 practice problems on arrays, strings, number logic and grids, with what Wipro's hiring pages say about Elite, Turbo and WILP.
- Cognizant GenC coding questions: 8 practice problems on strings, counting, stacks and greedy, with what Cognizant's pages say about GenC hiring.
- HR interview questions for freshers: 20 common questions, what each one is really asking, and sample answers in English and Hinglish.
- Tell me about yourself: sample answer for freshers: Three sample answers in English with a Hinglish version of each, what the interviewer is really asking and how to make it yours.
- DSA interview questions with solutions: 10 DSA questions students report from campus placements, with the idea, complexity and links to practice problems with tested code.