• Simulation Software 

    Get to know our software modules to

    Learn, grow, compete ,and outstand !!

Tcs Coding Questions 2021 ✪ (HOT)

Problem:
Two strings are anagrams if they contain same characters with same frequency.

Example:
Input: listen silent → Output: True

Solution (Python):

def are_anagrams(s1, s2):
    return sorted(s1) == sorted(s2)

s1 = input().strip() s2 = input().strip() print(are_anagrams(s1, s2))


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 Tcs Coding Questions 2021

Output format:
Minimum difference.

Sample Input:
7
7 3 2 4 9 12 56
3

Sample Output:
2


Asked in: TCS Digital (April 2021)

Problem Statement:
Given an array of integers (positive and negative), find the second smallest element and the second largest element without using any sorting algorithm or inbuilt sorting functions.

Constraints: Time Complexity O(N), Space Complexity O(1).

Example:

Approach (C++):

#include <iostream>
#include <climits>
using namespace std;

int main() int arr[] = 12, 35, 1, 10, 34, 1; int n = sizeof(arr)/sizeof(arr[0]);

int first_large = INT_MIN, second_large = INT_MIN;
int first_small = INT_MAX, second_small = INT_MAX;
for(int i = 0; i < n; i++) 
    // For largest
    if(arr[i] > first_large) 
        second_large = first_large;
        first_large = arr[i];
     else if(arr[i] > second_large && arr[i] != first_large) 
        second_large = arr[i];
// For smallest
    if(arr[i] < first_small) 
        second_small = first_small;
        first_small = arr[i];
     else if(arr[i] < second_small && arr[i] != first_small) 
        second_small = arr[i];
cout << "Second Largest: " << second_large << endl;
cout << "Second Smallest: " << second_small << endl;
return 0;

Problem Statement: Write a program to sort the first half of an array in ascending order and the second half in descending order.

Example:

Solution Logic:

Python Solution:

n = int(input())
arr = list(map(int, input().split()))
arr.sort()
mid = n // 2

Random practice won't help. Follow this 3-week plan:

Week 1: Brush up basics

Week 2: TCS-specific platforms

Week 3: Mock tests


Asked in: TCS Ninja (September 2021)

Problem Statement:
Write a program that takes an array of N integers. Calculate the sum of numbers present at even indices (0-based) and subtract the sum of numbers at odd indices. Return the absolute difference.

Example:

Approach (Python one-liner):

arr = list(map(int, input().split()))
even_sum = sum(arr[i] for i in range(0, len(arr), 2))
odd_sum = sum(arr[i] for i in range(1, len(arr), 2))
print(abs(even_sum - odd_sum))

TCS 2021 questions largely focused on these algorithms. Master these patterns, and you can solve almost any variation:

Problem Statement: Accept a string and an integer K. Shift every character in the string K positions forward in the alphabet. (Assume the string contains only lowercase alphabets). If the shift goes past 'z', wrap around to 'a'.

Input:

Output:

Example:

Input: abc 2 Output: cde

Solution (Python):

S = input().strip()
K = int(input())
res = ""
for char in S:
    # ASCII logic: 'a' is 97. Subtract 97 to get 0-25 range.
    # Add K, take modulo 26 to wrap around, add 97 back.
    new_char = chr(( (ord(char) - 97 + K) % 26) + 97)
    res += new_char
print(res)