# TCS Digital Coding Questions & Answer 2023 (Actual Questions): Previous, Expected Q/A, & More

## TCS Digital Coding Questions & Answer 2023 | TCS Digital Coding Questions Python | TCS Digital Coding Questions PDF | TCS Digital Advanced Coding Questions in Python | TCS Digital Advanced Coding Questions & Answer

TCS Digital Coding Questions & Answer 2023 | TCS Digital Questions: The coding test is of medium complexity, which implies that although it is not impossible to pass, it is also not particularly easy. You would surely have an edge in the next TCS Digital coding test if you practiced some of the code Questions from last year’s test. Let’s examine a few C, C++, Java, and Python, Test patterns, TCS Digital Coding Questions Python, code issues, and their remedies. Prior recruiting campaigns included a heavy emphasis on arrays and other basic programming concepts in their coding tests. With enough preparation, one may easily pass the first round and even subsequent rounds. To answer the coding tasks, you must pick a language that you are familiar with, such as C, C++, Java, or even Python, and use your programming skills. To get more private jobs, click here

## TCS Digital Coding Questions and Solutions

Q) Write a program that receives a word A and some texts as input. You need to output the texts (without modifying them) in the ascending order of the number of occurrences of the word A in the texts. The input is as follows: an integer M(between 1 and 100, inclusive), followed by the word A in the next line, and some text in each of the M next lines.

Note: The texts and the word A contain only lowercase Latin letters (a,b,c…,z) and blank spaces (“ ”). The maximum size of the texts and the word A is 100 Characters. Every text has a different number of occurrences of the word A.

Note 2:you must print one text per line without modifying the texts.

Example 1

• Input: 2
Java
I hate java
Python is a good programming language
• Output: Python is a good programming language
I hate java

Example 2

• Input:  3
python
I like to code in python
python is named after a show name monty python and not after the snake python
I think python is good i think python is important than php
• Output: i like to code in python
i think python is good i think python is important than php
python is named after a show name monty python and not after the snake python

C++ PROGRAM

```#include<bits/stdc++.h>
using namespace std;
string s;
map<string,int> m2;
bool cmp(pair<string, int>& a,
pair<string, int>& b)
{
return a.second < b.second;
}
void sort(map<string, int>& M)
{
vector<pair<string, int> > A;
for (auto& it : M) {
A.push_back(it);
}
sort(A.begin(), A.end(), cmp);
for (auto& it : A) {
for(int i=0;i<m2[it.first];i++)
cout << it.first <<endl; } } int count(string s1) { istringstream ss(s1); int c=0; while(ss) { string w; ss>>w;
if(w==s) c++;
}
return c;
}
int main()
{
int n;getline(cin,s);n=stoi(s);
getline(cin,s);
transform(s.begin(),s.end(),s.begin(),::tolower);
vector<string> v(n);
vector<int> a(n);
map<string,int> m;
for(int i=0;i<n;i++)
{
getline(cin,v[i]);
transform(v[i].begin(),v[i].end(),v[i].begin(),::tolower);
m2[v[i]]++;
m[v[i]]=count(v[i]);
}
sort(m);
}```

JAVA PROGRAM

```import java.util.*;
public class Main
{

static int countOccurences(String str, String word)
{
String a[] = str.split(" ");

int count = 0;
for (int i = 0; i < a.length; i++)
{
if (word.equals(a[i]))
count++;
}

return count;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String word=sc.next();
//System.out.println(word);
sc.nextLine();
String arr[]=new String[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLine();
//System.out.println(arr[i]);
}
TreeMap<Integer,String> map=new TreeMap<Integer,String>();
for(int i=0;i<n;i++)
{
map.put(countOccurences(arr[i],word),arr[i]);
}

Set s=map.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
Map.Entry m=(Map.Entry)itr.next();

System.out.println(m.getValue());
}

}
}```

PYTHON PROGRAM

```from collections import defaultdict
d=defaultdict(int)
d1=defaultdict(int)
n=int(input())
s=input()
s=s.lower()
l=[]
for i in range(n):
L=list(map(str,input().split()))
s1=' '.join(i for i in L)
c=0
for j in L:
if s==j:
c+=1
l.append(s1)
d[i]=c
d1[i]+=1
c=0
d=sorted(d.items(),key=lambda a:a[1])
for i in d:
for j in range(d1[i[0]]):
print(l[i[0]])```

Q) Write a program that will print the sum of diagonal elements of a 10X10 matrix. The program will take a total of 100 numbers as input (10 numbers will be input per line and each number will be separated by a space).

Example 1

• Input:    1  2 3 4 5 6 7 8 9 0
0 1 2 3 4 5 6 7 8 0
3 4 5 6 7 8 9 6 4 0
2 3 4 5 6 7 8 9 3 2
3 4 5 6 7 4 3 2 1 3
3 4 5 6 2 4 4 2 4 6
2 3 4 6 2 4 6 2 3 5
2 3 5 6 2 4 6 2 3 5
2 4 6 2 1 4 3 3 5 2
3 3 5 2 4 6 2 1 4 6
• Output:  42

Example 2

• Input:   1 22 33 44 55 66 77 88 99 100
100 1 88 77 66 55 44 33 22 11
88 88 1 66 55 44 33 22 11 100
88 77 66 1 44 33 22 11 100 99
77 66 55 44  1 22  11 88 99 100
66 55 44 33 22 1 77 88 99 100
44 33 22 11 100 99 1 77 66 55
33 22 11 100 99 88 77 1 55 44
22 11 100 99 88 77 66 55 1 33
100 11 22 33 44 55 99 88 77 1
• Output: 10

C++ PROGRAM

```#include<bits/stdc++.h>
using namespace std;

int main()
{
vector<vector<int>> v(10,vector<int>(10));
for(int i=0;i<10;i++)
for(int j=0;j<10;j++) cin>>v[i][j];
int sum=0;

for(int i=0;i<10;i++)
sum+=v[i][i];
cout<<sum;
}```

JAVA PROGRAM

```import java.util.*;
public class Main
{
public static boolean isVowel(char ch)
{
return ch=='a' || ch=='e' || ch=='o' || ch=='i' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U';
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
char arr[]=str.toCharArray();
String res="";
if(!isVowel(arr[0]))
res+=arr[0];
for(int i=1;i<arr.length;i++)
{
if(isVowel(arr[i])&&isVowel(arr[i-1]))
res+=arr[i-1]+""+arr[i];
if(!isVowel(arr[i]))
res+=arr[i];
}
System.out.println(res);
}```

PYTHON PROGRAM

```l=['a','e','i','o','u','A','E','I','O','U']
s=input()
s+=" "
s1=""
if len(s)==0:
print("")
elif len(s)==1:
if s[0] not in l:
print(s)
else:
if s[0] in l and s[1] in l:
s1+=s[0]
elif s[0] not in l :
s1+=s[0]

for i in range(1,len(s)-1):
if s[i-1] not in l and s[i] in l and s[i+1] not in l:
continue
s1+=s[i]

print(s1)```

Q) Write a program that will take one string as input. The program will then remove vowels a, e, i, o, and u (in lower or upper case ) from the string. If there are two or more vowels that occur together then the program shall ignore all of those vowels.

Example 1

• Input:  Cat
• Output:  Ct

Example 2

• Input:  Compuuter
• Output: Cmpuutr

C++  PROGRAM

```#include<bits/stdc++.h>
using namespace std;int main()
{
string s;
int c=0;
getline(cin,s);
for (auto i:s)
{
if(i=='(') c++;
if(i==')') c--;
}
cout<<(c==0);
}```

PYTHON PROGRAM

```s=input()
c=0
for i in s:
if i =='(':
c+=1
elif (i==')') and c>0 :
c-=1
print(int(c>0))```

Q) Write a program to find out and display prime numbers from the given list of integers. The program will accept input in two lines. First-line contains a number indicating the total number of integers in the list and the second line contains integers separated by spaces.

Example 1

• Input: 5
4 6 9 3 7
• Output:  3 7

Example 2

• Input:  10
8 10 3 12 7 15 11 2 17 26
• Output:  3 7 11 2 17

C++  PROGRAM

```#include<bits/stdc++.h>
using namespace std;
map<int,int> m;
bool ifPrime(int a)
{
if(m[a]==2||m[a]==1) return m[a]-1;
if((a&1)==0) {m[a]=1;return false;}
for(int i=3;i<=sqrt(a);i+=2)
if(a%i==0) {m[a]=1;return 0;}
m[a]=2;
return 1;
}
int main()
{
m[2]=2;
int n,a;cin>>n;
for(int i=0;i<n;i++)
{
cin>>a;
if(ifPrime(a)) cout<<a<<" ";
}
}```

## TCS Digital Coding Questions Python

Question: Parallel Columbus

Problem Statement: Nobel Prize-winning Austrian-Irish physicist Erwin Schrödinger developed a machine and brought as many Christopher Columbus from as many parallel universes he could. Actually he was quite amused by the fact that Columbus tried to find India and got America. He planned to dig it further.

Though totally for research purposes, he made a grid of size n X m, and planted some people of America in a position (x,y) [in 1 based indexing of the grid], and then planted you with some of your friends in the (n,m) position of the grid. Now he gathered all the Columbus in 1,1 positions and started a race.

Given the values for n, m, x, y, you have to tell how many different Columbus(s) together will explore you as India for the first time.

Remember, the Columbus who will reach to the people of America, will be thinking that as India and hence wont come further.

Function Description:

Complete the markgame function in the editor below. It has the following parameter(s):

Parameters:

 Name Type Description n Integer The number of rows in the grid. m Integer The number of columns in the grid. x Integer The American cell’s Row. y Integer The American cell’s Column.

Constraints:

• 1 <= n <= 10^2
• 1 <= m <= 10^2
• 1 <= x <= n
• 1 <= y <= m

Input Format:

• The first line contains an integer, n, denoting the number of rows in the grid.
• The next line contains an integer m, denoting the number of columns in the grid.
• The next line contains an integer, x, denoting the American cell’s row.
• The next line contains an integer, y, denoting the American cell’s column.

Sample Cases

Sample Input 1

2

2

2

1

Sample Output 1

1

Explanation

The only way possible is (1,1) ->(2,1) -> (2,2), so the answer is 1.

C++ PROGRAM

```#include <bits/stdc++.h>
using namespace std;
unordered_map<int,long long int> f;

long long int Fact(int n)
{
if(f[n]) return f[n];
return f[n]=n*Fact(n-1);
}

int main()
{
int n,m,x,y;
cin>>n>>m>>x>>y;
n-=1;m-=1;x-=1;y-=1;
f[0]=f[1]=1;
int p=(Fact(m+n)/(Fact(m)*Fact(n)));
int imp=((Fact(x+y)/(Fact(x)*Fact(y)))*(Fact(m-x+n-y)/(Fact(m-x)*Fact(n-y))));
cout<<p-imp;
}```

PYTHON PROGRAM

```import math
n=int(input())-1
m=int(input())-1
x=int(input())-1
y=int(input())-1

ans=math.factorial(n+m)
ans=ans//(math.factorial(n))
ans=ans//(math.factorial(m))

ans1=math.factorial(x+y)
ans1=ans1//(math.factorial(x))
ans1=ans1//(math.factorial(y))

x1=n-x
y1=m-y

ans2=math.factorial(x1+y1)
ans2=ans2//(math.factorial(x1))
ans2=ans2//(math.factorial(y1))
print(ans-(ans1*ans2))```

Question: Amusement Park

Problem Statement: Aashay loves to go to WONDERLA , an amusement park. They are offering students who can code well with some discount. Our task is to reduce the cost of the ticket as low as possible.

They will give some k turns to remove the cost of one ticket where the cost of tickets are combined and given as string. Help Aashay in coding as he is not good in programming and get a 50%  discount for your ticket.

Constraints:

• 1 <= number of tickets <= 10^5
• 1 <= K <= number of tickets

Input Format for Custom Testing:

• The first line contains a string,Tickets, denoting the given cost of each ticket.
• The next line contains an integer, K, denoting the number of tickets that is to be removed.

Sample Cases:

• Sample Input 1
203
3
• Sample Output 1
0

C++ PROGRAM

```#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int smallestNumber (string num, int k)
{
if(num.length()<=k)
return 0;
unordered_map<char,int> pos;
for(int i=0;i<num.length();i++)
{ pos[num[i]]=i;}

string temp=num;
sort(num.begin(),num.end());
string ans=num.substr(0,num.length()-k);
vector<int> v;
for(int i=0;i<ans.length();i++)
v.push_back(pos[ans[i]]);

sort(v.begin(),v.end());
string ret;
for(int i=0;i<v.size();i++)
{ret+=temp[v[i]];}
int final=stoi(ret);

return final;
}
int main()
{
string s;
cin >> s;
int k;
cin >> k;
int ans;
cout<<smallestNumber(s,k)%(int)(pow(10,9)+7);
return 0;
}```

PYTHON PROGRAM

```import sys
n=input()
k=int(input())
n1=len(n)
if len(n)<=k:
print(0)
sys.exit()

a=''
i=0
while i<(n1-1) and k>0:
if int(n[i])>int(n[i+1]):
i+=1
k-=1
continue

else:
a+=n[i]
i+=1

a+=n[i]
i+=1
if k>0:
a=a[:-k]

if i<=(n1-1):
while i<n1:
a+=n[i]
i+=1

print(int(a)%((10**9)+7))```

## TCS Digital Advanced Coding Questions in Python

Questions: Andy wants to go on a vacation to de-stress himself. Therefore he decides to take a trip to an island. It is given that he has as many consecutive days as possible to rest, but he can only make one trip to the island. Suppose that the days are numbered from 1 to N. Andy has M obligations in his schedule, which he has already undertaken and which correspond to some specific days. This means that ith obligation is scheduled for day Di. Andy is willing to cancel at most k of his obligations in order to take more holidays.

Your task is to find out the maximum days of vacation Andy can take by canceling at most K of his obligations.

Input Format

• The first line contains an integer N, denoting the total number of days
• The next line contains an integer M denoting the total number of obligations.
• The next line contains an integer K denoting the largest number of obligations he could cancel
• Each line i of the M subsequent lines (where 0<=i<=M) contains an integer describing Di.

Constraints

• 1<=N<=10^6
• 1<=M<=2*10^6
• 1<=K<=2*10^6
• 1<=D[i]<=10^6

Sample Input 1:

10
5
2
6
9
3
2
7

Sample Output 1 :

5

Explanation:

Here he could cancel his 3rd and 4th obligation which makes vacation length 5.

Sample input 2:

7
2
0
3
4

Sample Output 2:

3

Explanation:

Here he could not cancel any obligation since K=0, so the vacation length is 3.

### EXAMPLE PROGRAM

PYTHON PROGRAM

```n = int(input())
m = int(input())
k = int(input())
arr = [0] * n
for i in range(m):
arr[i] = int(input())
ans = 0
arr.sort()
if k > 0:
for i in range(k + 1, m + 3, 1):
ans = max(ans, arr[i] - arr[i - k - 1] - 1)
else:
j = 0
while arr[j] == 0:
j = j + 1
count = 0
for i in range(1, n + 1, 1):
count += 1
if j < n and (i == arr[j]):
count = 0

j += 1
ans = max(count, ans)
print(ans)```

Q) Khaled has an array A of N elements. It is guaranteed that N is even. He wants to choose at most N/2 elements from array A. It is not necessary to choose consecutive elements.  Khaled is interested in XOR of all the elements he chooses. Here, XOR denotes the bitwise XOR operation.

For example:

• If A=[2,4,6,8], then khaled can choose the subset [2,4,8] to achieve XOR=(2 XOR 4 XOR 8)=14.

Khaled wants to maximize the XOR of all the elements he chooses. Your task is to help khaled to find the max XOR of a subset that he can achieve by choosing at most N/2 elements?

Input format:

• The first line contains an integer, N, denoting the number of elements in A.
• Each line i of the N subsequent lines(where 0<=i<=N) contains an integer describing Ai.

Constraints

• 1<=N<=120
• 1<=A[i]<=10^6

Sample Input 1

2
1
2
Sample Output 1
2

Explanation:

N=2,  A=[1,2] khaled can choose the subset[2]. The xor of the elements in the subset is 2. And the number of elements in the subset is 1 which is less than N/2.

Sample Input 2
4
1
2
4
7

Sample Output 2

7

Explanation:

N=4,  A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements in the subset is 7, and the number of elements in the subset is 1 which is less than N/2.

### EXAMPLE PROGRAMS

PYTHON PROGRAM

```from itertools import combinations

def fun(arr, N):
sub = []
max_xor = max(arr)
for i in range(1, N // 2):
comb = combinations(arr, i + 1)
for i in comb:
sub.append(list(i))
for a in sub:
z = 0
for b in a:
z = z ^ b
if z > max_xor:
max_xor = z
return max_xor

N = int(input("Enter Length : "))
arr = []
for i in range(N):
arr.append(int(input(f"Enter {i+1} Element : ")))
if N < 3:
print("Output :", max(arr))
else:
print("Output :", fun(arr, N))```

Q) Write a program to calculate and return the sum of absolute difference between the adjacent number in an array of positive integers from the position entered by the user.

You are expected to write code in the findTotalSum function only which receive three positional arguments:

1st: number of elements in the array
2nd: Array
3rd: position from where the sum is to be calculated

Example

Input
input 1 : 7
input 2 : 11 22 12 24 13 26 14
input 3 : 5

Output
25

Explanation

The first parameter 7 is the size of the array. Next is an array of integers and input 5 is the position from where you have to calculate the Total Sum. The output  is 25 as per calculation below.
| 26-13 | = 13
| 14-26 | =  12
Total Sum = 13 + 12 = 25

Here is the code:

```def findTotalSum(n,numbers,pos):
total = 0
for i in range(pos-1,n-1):
total+= abs(numbers[i]-numbers[i+1])
return total
n = int(input())
numbers = list(map(int, input().split()))
pos = int(input())
print(findTotalSum(n,numbers,pos))```

Q) Write a program to find the difference between the elements at odd index and even index.

Note : You are expected to write code in the findDifference function only which receive the first parameter as the numbers of items in the array and second parameter as the array itself. You are not required to take the input from the console.

Example

Finding the maximum difference between adjacent items of a list of 5 numbers

Input
input 1 : 7
input 2 : 10 20 30 40 50 60 70

Output
40

Explanation
The first parameter 7 is the size of the array. Sum of element at even index of array is 10 + 30 + 50 + 70 = 160 and sum of elements at odd index of array is 20 + 40 + 60 = 120. The difference between both is 40

Here is the code:

```def findDifference(n,values):
total = 0
for i in range(n):
if i%2 == 0:
total+=values[i]
else:
total-=values[i]
return total
n = int(input())
values = list(map(int, input().split()))
print(findDifference(n,values))```

***BEST OFF LUCK***