# TCS Ninja Coding Questions With Answers 2024 (Fresher/Exp) | TCS Ninja Previous Year Question Paper

## TCS Ninja Coding Questions With Answers 2024 | TCS Ninja Previous Year Question Paper PDF | TCS Ninja Coding Previous Year Question | TCS Ninja Coding MCQ Questions PDF | TCS Ninja Coding Questions & Answers PDF

TCS Ninja Coding Questions With Answers 2024: The TCS Ninja Coding Questions cover every basic algorithm, data structure, and programming topic. We can assist you if you’re interested in working at TCS Ninja but are unsure about how to get ready for the questions in the TCS Ninja Coding Round. One of the most important sections of the TCS Ninja exam is the coding round. We discussed several possible solutions and examples of TCS Ninja Coding rounds in this article. In this post, we will discuss TCS Ninja Coding Questions

A unique technique for assessing a candidate’s capacity for problem-solving is offered by TCS Ninja. On their blog, they frequently hold coding competitions. Should you successfully finish the challenge, you might receive an interview request. This tutorial will teach you all there is to know about TCS Ninja. More details like TCS Ninja Coding Questions and Answers, TCS Ninja Coding Questions and Answers, TCS Ninja Interview Questions and Answers, TCS Ninja Cut Off, Salary, Hike, Increment, Annual Appraisal, etc. are available on this page

### TCS Ninja Coding Model Questions With Answers For Freshers

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<<" ";
}
}```

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)```

### TCS Ninja Coding Previous Questions With Answers

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))

Keep an eye on for the most up-to-date information about upcoming exams and results. Find out about official notices, Exam Patterns, Syllabus, Previous Question Papers, Expected Cut Off, Results, Merit List, Study Materials, and much more. Now is the time to bookmark in order to take advantage of even more amazing career opportunities.

### Govt Jobs by Qualifications

Education & Vacancies Salary Apply Link
10th Pass Govt Jobs - 5,000 Vacancies Rs. 5,200 - 63,200 Apply Now
12th Pass Govt Jobs - 18,000+ Vacancies Rs. 5,200 - 92,300 Apply Now
ITI Pass Jobs - 3,500 Vacancies Rs. 5,200 - 35,000 Apply Now
Any Graduate Jobs - 19,100 Vacancies Rs. 5,200 - 92,300 Apply Now
Central Govt Jobs Rs. 5,200 - 17,000 Apply Now
Bank Jobs - 1,000 Vacancies Rs. 5,200 - 29,200 Apply Now
Diploma Jobs - 9,300 Vacancies Rs. 5,200 - 35,000 Apply Now
BTech/BE Jobs - 18,000 Vacancies Rs. 15,000 - 1,00,000 Apply Now
Data Entry Jobs - 1,300 Vacancies Rs. 5,200 - 29,200 Apply Now
Private Jobs Rs. 10,000 - 67,700 Apply Now