Tcs Coding Questions 2021 May 2026
Example N=4:
1
2 3
4 5 6
7 8 9 10
Concept: Counter variable inside nested loops.
Example: Input 123 → Output 321 | Input -456 → Output -654
Concept: Use modulo 10 to extract digits, build reversed number.
Difficulty: Medium-Hard
Marks: 18
Statement:
Given an array of N integers where each value represents the number of chocolates in a packet. There are M children. Distribute M packets such that:
Print that minimum difference.
Example:
arr = [7, 3, 2, 4, 9, 12, 56], M = 3
Sorted: [2,3,4,7,9,12,56]
Possible windows of size 3:
(2,3,4) diff=2, (3,4,7) diff=4, (4,7,9) diff=5, (7,9,12) diff=5, (9,12,56) diff=47
Minimum = 2.
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ M ≤ N
1 ≤ chocolates ≤ 10⁹
Input format:
First line: N
Second line: N integers
Third line: M
Output format:
Minimum difference.
Sample Input:
7
7 3 2 4 9 12 56
3
Sample Output:
2
Based on student memory reports and discussion forums (like PrepInsta, GeeksforGeeks, Quora), the following topics dominated:
Difficulty: Easy
Marks: 10
Statement:
Given a string containing alphabets and special characters (e.g., @, #, $, !), reverse only the alphabetic characters while keeping special characters in their original positions.
Example:
Input: "a,b$c"
Output: "c,b$a"
Constraints:
1 ≤ length of string ≤ 10⁵
Input format:
Single line string S.
Output format:
Modified string.
Sample Input:
"A@b#c$D"
Sample Output:
"D@c#b$A"
Problem Statement: You are given an array of integers. Write a program to check if there are three consecutive even numbers or three consecutive odd numbers in the array. If found, print "True", otherwise print "False".
Input:
Output:
Example:
Input: 5 1 2 4 6 8 Output: True (Logic: 2, 4, 6 are three consecutive even numbers)
Solution (C++):
#include <iostream>
using namespace std;
int main()
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
int evenCount = 0, oddCount = 0;
int flag = 0;
for(int i = 0; i < n; i++)
if(arr[i] % 2 == 0)
evenCount++;
oddCount = 0;
else
oddCount++;
evenCount = 0;
if(evenCount == 3
if(flag == 1) cout << "True";
else cout << "False";
return 0;
Here are the most frequently asked patterns, reconstructed from student memory and forums like PrepInsta, GeeksforGeeks, and CodeChef discussions. Tcs Coding Questions 2021
Difficulty: Medium
Marks: 15
Statement:
Given an array of N integers and an integer K, find the number of unique pairs (i, j) with i < j such that arr[i] + arr[j] = K.
Example:
Input: arr = [1, 5, 7, 1, 5], K = 6
Pairs: (1,5) at indices (0,1) and (0,3), but unique values?
Here values (1,5) is one pair. So answer = 1.
Wait – clarify:
Better example: arr = [1, 2, 3, 4, 3], K = 6
Pairs: (2,4) and (3,3) → 2 pairs.
Constraints:
1 ≤ N ≤ 10⁵
-10⁹ ≤ arr[i], K ≤ 10⁹
Input format:
First line: N K
Second line: N integers.
Output format:
Integer count.
Sample Input:
5 6
1 2 3 4 3
Sample Output:
2