Capgemini Coding Questions: 4 Patterns with Step-by-Step Solutions

Learn four transferable coding patterns through constructed Capgemini preparation examples. Each solution includes an invariant, complete trace, complexity and edge-case checks.

KnowledgeGate Team

Exam prep & CS education

Updated 19 Oct 20266 min read

Memorising an isolated solution fails as soon as an array, string or constraint changes. This guide builds four reusable patterns through exact inputs, invariants, traces, outputs and complexity checks. They are constructed preparation examples, not a fixed or official Capgemini question list. Use Company-Specific Placement Courses to locate company-focused preparation alongside this method practice.

Capgemini coding questions: recognise the pattern before writing code

A pattern is a reusable relationship between the input, the required output and an invariant that remains true while the algorithm runs. Start by reading the contract, writing a tiny example and naming that invariant. Then choose the data structure, trace it once and only then code.

Signal in the problem

Likely pattern

Expected complexity

preserve order while moving selected values

two pointers

O(n)

longest contiguous segment satisfying a rule

sliding window plus last-seen map

O(n)

nearest greater value to the right

monotonic stack

O(n)

maximum non-overlapping activities

sort by finish time plus greedy selection

O(n log n)

The Capgemini Superset course is an optional company-focused route with programming and DSA practice, but it does not predict a live assessment or promise these exact prompts.

Two pointers: move zeroes without changing non-zero order

Constructed prompt: Given a = [0, 3, 0, 1, 4, 0, 2], move every zero to the end while preserving the order of non-zero values. Modify the array in place with O(1) auxiliary space.

The invariant is: before each read step, positions 0 through write-1 contain every non-zero value seen so far, in original order. Start with write=0. At read=1, write 3 at index 0, then set write=1. At read=3, write 1 at index 1. At read=4, write 4 at index 2. At read=6, write 2 at index 3. Finally, fill indices 4, 5 and 6 with zero. The result is [3, 1, 4, 2, 0, 0, 0].

Code
write = 0
for read from 0 to n-1:
    if a[read] != 0:
        a[write] = a[read]
        write = write + 1
while write < n:
    a[write] = 0
    write = write + 1

Time is O(n) and auxiliary space is O(1). Check [0,0] -> [0,0], [5,4] -> [5,4] and [] -> [].

Sliding window: longest substring without repeated characters

Constructed prompt: For s = "abcaebbc", return the length of the longest contiguous substring with no repeated character.

Keep the left boundary of the valid window and the latest index of each character. When ch has appeared, set left = max(left, lastSeen[ch] + 1). Then set lastSeen[ch] = right and update the best length. The invariant is that the window from left to right has no repeated character after each update.

right

char

previous index

left after update

current window

best length

0

a

-

0

"a"

1

1

b

-

0

"ab"

2

2

c

-

0

"abc"

3

3

a

0

1

"bca"

3

4

e

-

1

"bcae"

4

5

b

1

2

"caeb"

4

6

b

5

6

"b"

4

7

c

2

6

"bc"

4

The answer is 4. The maximum windows are "bcae" at indices 1..4 and "caeb" at 2..5. Each character enters the right side once, so time is O(n) and space is O(k) for k distinct characters. For "abba", the final a was last seen at 0; max prevents left moving backwards from 2 to 1.

Sliding-window trace for s="abcaebbc". Show character indices exactly 0:a, 1:b, 2:c, 3:a, 4:e, 5:b, 6:b, 7:c; show left after each right index as 0,0,0,1,1,2,6,6; highlight the two maximum windows bcae at 1..4 and caeb at 2..5, each length 4; add the rule left=max(left,lastSeen[ch]+1) and the final answer 4. Do not add another string, index, window or answer.

Monotonic stack: find the next greater element in one pass

Constructed prompt: For a = [4, 5, 2, 10, 8], return the first strictly greater value to the right of each element, or -1 when none exists.

The stack holds unresolved indices whose next greater value has not appeared. Read index 0, value 4, and push 0. At index 1, value 5, pop 0, set answer 0=5, then push 1. At index 2, value 2, push 2. At index 3, value 10, pop 2 and set answer 2=10; pop 1 and set answer 1=10; then push 3. At index 4, value 8, push 4. Indices 3 and 4 remain unresolved, producing [5, 10, 10, -1, -1].

Code
answer = array filled with -1
for i from 0 to n-1:
    while stack not empty and a[i] > a[stack.top]:
        answer[stack.pop] = a[i]
    stack.push(i)

Each index is pushed once and popped at most once, so time is O(n) and space is O(n). With [2,2], the answer is [-1,-1]. Using >= in the pop condition would wrongly treat equality as greater.

Greedy scheduling: maximise non-overlapping activities

Constructed prompt: activities are A(1,3), B(2,5), C(4,7), D(1,8), E(6,9), F(8,10), where a new activity is compatible when start >= lastFinish. Choose the maximum number of non-overlapping activities.

Sort by finish time: A(1,3), B(2,5), C(4,7), D(1,8), E(6,9), F(8,10). Select A, making lastFinish=3. Reject B because 2<3. Select C because 4>=3, making lastFinish=7. Reject D because 1<7, and reject E because 6<7. Select F because 8>=7, making lastFinish=10. Return [A,C,F], count 3.

The invariant is that the selected activities do not overlap and the current selection finishes as early as possible among choices of its size. Choosing the compatible activity that finishes earliest leaves at least as much room for later work as a later-finishing choice.

Code
sort activities by finish time
lastFinish = -infinity
for activity in sorted order:
    if activity.start >= lastFinish:
        select activity
        lastFinish = activity.finish

Sorting costs O(n log n) and the scan costs O(n). Extra scan space is O(1), apart from output, if sorting is in place. Sorting by start time or shortest duration is not the same rule.

Greedy activity-selection timeline from time 1 through 10. Draw exactly A(1,3), B(2,5), C(4,7), D(1,8), E(6,9), F(8,10); colour selected A, C, F in orange and rejected B, D, E in grey; annotate the selected finish transitions lastFinish: 3 -> 7 -> 10 and the decisions B: 2<3 reject, C: 4>=3 select, D: 1<7 reject, E: 6<7 reject, F: 8>=7 select; show final result [A,C,F], count 3. Do not invent another interval, time value or rule.

Coding-question traps: test the contract, not only the sample

Use small adversarial cases before trusting the sample output.

Pattern

Test

Expected result

move zeroes

[0,0,0]

[0,0,0]

move zeroes

[7]

[7]

longest unique substring

""

0

longest unique substring

"aaaa"

1

next greater

[5,4,3]

[-1,-1,-1]

interval scheduling

(1,2),(2,3),(3,4)

all 3

Common failures have direct repairs. Define the boundary rule before coding. Never reset a sliding-window pointer backwards. Store indices, not only values, when answers belong to positions. Report complexity only after checking the constraints. Do one hand trace and one adversarial edge case for every solution.

For another independent practice set, TCS Coding Questions: 4 Patterns with Solved Dry Runs covers direct simulation, second-largest distinct value, order-preserving frequency and equilibrium index. This article covers stable compaction, sliding windows, monotonic stacks and activity selection.

How Capgemini may assess coding and what the official page actually says

The official Capgemini India recruitment-process page says proctored online technical and language assessments may apply where applicable. It does not define a universal four-pattern coding syllabus, allowed language, question count, duration, score or cutoff. The current role page, invitation, assessment portal and candidate instructions control.

Use a format-independent loop: parse the input and output, write the invariant beside the function, solve a tiny case by hand, implement, then test relevant empty, duplicate and boundary cases. For optional depth, KnowledgeGate has 150+ published Capgemini-tagged questions. That is practice inventory, not evidence of frequency. The Capgemini technical interview guide is the next-stage hand-off for pseudocode, CS fundamentals and projects.

Capgemini coding questions in the short version

Use this retrieval line: contract -> pattern -> invariant -> dry run -> complexity -> edge cases. The four results are zero compaction [3,1,4,2,0,0,0], unique-window length 4, next-greater [5,10,10,-1,-1], and activities [A,C,F], count 3.

Now cover the traces, re-solve all four prompts, change one input and predict the new output before running code. The goal is method transfer, not answer memory. The Coding for Placements course is a broader optional route for language, DSA, hashing, greedy methods and complexity practice.