TCS Coding questions 2023-2024 | TCS Ninja to Digital Coding Questions with Answers | TCS Prime Role Coding Question 2024: If anyone have exam TCS NQT exam in upcoming month and you are looking for TCS Previous years question and answer then you are landed on right place.
As you gear up for the upcoming TCS NQT exam next month, our platform offers an exclusive collection of TCS coding questions spanning from TCS Ninja to Digital Coding for the years 2023-2024. Enhance your preparation and guarantee success with our comprehensive set of questions, complemented by detailed answers. Don’t miss out on this invaluable resource to excel in the TCS NQT exam!
TCS Exam Pattern/Selection /Recruitment process for for Ninja Digital & Prime Roles:
Join Telegram Channel (43,230 Member)Question 1: TCS Coding questions 2023-2024:
Problem Statement -: Given an array Arr[ ] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K.
Note : Keep the first of the array unaltered.
Example 1:
- 5 —Value of N
- {10, 20, 30, 40, 50} —Element of Arr[ ]
- 2 —–Value of K
Output : 40 50 10 20 30
Example 2:
- 4 —Value of N
- {10, 20, 30, 40} —Element of Arr[]
- 1 —–Value of K
Output : 40 10 20 30
Solution of question 1 in C++:
#include<bits/stdc++.h>
using namespace std;
vector rotate(int nums[], int n, int k) {
if (k > n)
k = k % n;
vector ans(n);
for (int i = 0; i < k; i++) {
ans[i] = nums[n - k + i];
}
int index = 0;
for (int i = k; i < n; i++) {
ans[i] = nums[index++];
}
return ans;
}
int main()
{
int Array[] = { 1, 2, 3, 4, 5 };
int N = sizeof(Array) / sizeof(Array[0]);
int K = 2;
vector ans = rotate(Array, N, K);
for (int i = 0; i < N; ++i) {
cout << ans[i] << ' ';
}
}
Solution of question 1 in JAVA:
import java.util.*;
public class Main
{
static int[] rotate(int nums[], int n, int k) {
if (k > n)
k = k % n;
int[] ans = new int[n];
for (int i = 0; i < k; i++) {
ans[i] = nums[n - k + i];
}
int index = 0;
for (int i = k; i < n; i++) {
ans[i] = nums[index++];
}
return ans;
}
public static void main(String[] args) {
int Array[] = { 1, 2, 3, 4, 5 };
int N = 5;
int K = 2;
int[] ans = rotate(Array, N, K);
for (int i = 0; i < N; ++i) {
System.out.println(ans[i]);
}
}
}
Question 2: TCS Ninja Coding Questions 2022-2023:
Problem Statement -: Given two non-negative integers n1 and n2, where n1
For example: Suppose n1=11 and n2=15.
There is the number 11, which has repeated digits, but 12, 13, 14 and 15 have no repeated digits. So, the output is 4.
Example1:
Input:
- 11 — Vlaue of n1
- 15 — value of n2
Output: 4
Example 2:
Input:
- 101 — value of n1
- 200 — value of n2
Output: 72
Solution of question 2 in C++:
#include<bits/stdc++.h>
using namespace std;
int find(int n1, int n2) {
int count = 0;
for (int i = n1 ; i <= n2 ; i++) {
int num = i;
vector visited;
visited.assign(10, false);
while (num > 0) {
if (visited[num % 10] == true)
break;
visited[num % 10] = true;
num /= 10;
}
if (num == 0)
count++;
}
return count;
}
int main()
{
int n1 = 101, n2 = 200;
cout << find(n1, n2);
}
Solution of question 2 in JAVA:
import java.util.*;
public class Main
{
static int find(int n1, int n2) {
int count = 0;
for (int i = n1 ; i <= n2 ; i++) { int num = i; boolean[] visited = new boolean[10]; while (num > 0) {
if (visited[num % 10] == true)
break;
visited[num % 10] = true;
num /= 10;
}
if (num == 0)
count++;
}
return count;
}
public static void main(String[] args) {
int n1 = 101, n2 = 200;
System.out.println(find(n1, n2));
}
}
Question 3: tcs digital coding questions 2023:
Problem Statement -: In this 3 Palindrome, Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes.
Similarly, choose the smallest second palindromic substring that leaves a third palindromic substring.
If there is no way to split the word into exactly three palindromic substrings, print “Impossible” (without quotes). Every character of the string needs to be consumed.
Cases not allowed –
- After finding 3 palindromes using above instructions, if any character of the original string remains unconsumed.
- No character may be shared in forming 3 palindromes.
Constraints
- 1 <= the length of input sting <= 1000
Input: First line contains the input string consisting of characters between [a-z].
Output: Print 3 substrings one on each line.
Time Limit
1
Examples
Example 1
Input
nayannamantenet
Output
nayan
naman
tenet
Explanation
- The original string can be split into 3 palindromes as mentioned in the output.
- However, if the input was nayanamantenet, then the answer would be “Impossible”.
Example 2
Input
aaaaa
Output
a
a
aaa
Explanation
- The other ways to split the given string into 3 palindromes are as follows –
- [a, aaa, a], [aaa, a, a], [aa, aa, a], etc.
- Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, a, aaa].
Example 3
Input
aaaabaaaa
Output
a
aaabaaa
a
Explanation
- The other ways to split the given string into 3 palindromes are as follows –
- [aaaa, b, aaaa], [aa, aabaa, aa], etc.
- Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, aaabaaa, a].
Solution of question 3 in C++:
#include<bits/stdc++.h>
typedef long long int lld;
#define mod 1000000007
using namespace std;
bool pali(string s)
{
if(s.length()==1) return true;
string s1=s;reverse(s1.begin(),s1.end());
return (s1==s);
}
int main()
{
speed;
string s,s1,s2,s3;
cin>>s;
int l=s.length();
for(int i=1;i<l-1;i++)
{
s1=s.substr(0,i);
if(pali(s1))
for(int j=1;j<l-i;j++)
{
s2=s.substr(i,j);s3=s.substr(i+j,l-i-j);
if(pali(s2)&&pali(s3))
{
cout<<s1<<endl<<s2<<endl<<s3;return 0;
}
}
}
cout<<"Impossible";
return 0;
}
Solution of question 3 in Python:
import sys
def if_palindrome(s):
if len(s)==1:
return True
s1=s[::-1]
return s1==s
s=input()
l=len(s)
for i in range(1,l-1):
s1=s[:i]
if if_palindrome(s1):
for j in range(1,l-i):
s2=s[i:i+j]
s3=s[i+j:]
if if_palindrome(s2) and if_palindrome(s3):
print(s1)
print(s2)
print(s3)
sys.exit()
print("Impossible")
Question 4: TCS Ninja to Digital Coding Questions with Answers/Solution
Problem Statement -: In this even odd problem Given a range [low, high] (both inclusive), select K numbers from the range (a number can be chosen multiple times) such that sum of those K numbers is even.
Calculate the number of all such permutations. As this number can be large, print it modulo (1e9 +7).
Constraints
- 0 <= low <= high <= 10^9
- K <= 10^6.
Input
- First line contains two space separated integers denoting low and high respectively
- Second line contains a single integer K.
Output
- Print a single integer denoting the number of all such permutations
Time Limit: 1
Examples
Example 1
Input
4 5
3
Output: 4
Explanation
There are 4 valid permutations viz. {4, 4, 4}, {4, 5, 5}, {5, 4, 5} and {5, 5, 4} which sum up to an even number.
Example 2
Input
1 10
2
Output: 50
Explanation
There are 50 valid permutations viz. {1,1}, {1, 3},.. {1, 9} {2,2}, {2, 4},… {2, 10} . . . {10, 2}, {10, 4},… {10, 10}. These 50 permutations, each sum up to an even number.
Solution of question 4 in Python:
mod=1000000007
def esum(m,n,K,N):
if(K==1):
return n
else:
return (N-(m-n)*esum(m,n,K-1,N)%mod)
low,high=map(int,input().split())
K=int(input())
diff=high-low+1
if(diff%2==0):
m=diff//2
n=m
else:
if(low%2==0):
m=(diff-1)//2
n=m+1
else:
m=(diff+1)//2
n=m-1
N=m
for i in range(K-1):
N=(N*(m+n))%mod
out=esum(m,n,K,N)%mod
print(out)
Solution of question 4 in C++:
#include<bits/stdc++.h>
using namespace std;
typedef long long int lld;
#define mod 1000000007
long e_sum(long m,long n,long K,long N)
{
if(K==1)
{
return n;
}
else
{
return (N-(m-n)*e_sum(m,n,K-1,N)%(1000000007));
}
}
int main()
{
long low,high,K,m,n,diff,Out,N,i;
scanf("%ld",&low);
scanf("%ld",&high);
scanf("%ld",&K);
diff=high-low+1;
if(diff%2==0)
{
m=diff/2;
n=m;
}
else
{
if(low%2==0)
{
m=(diff-1)/2;
n=m+1;
}
else
{
m=(diff+1)/2;
n=m-1;
}
}
N=m;
for(i=0;i<K-1;i++)
{
N=(N*(m+n))%1000000007;
}
Out=e_sum(m,n,K,N)%1000000007;
printf("%ld",Out);
return 0;
}
For Daily Jobs & Internship Update Follow Us:
Join Telegram (Must) | Click Here |
Join Experienced Job Telegram | Click Here |
Follow Instagram Job Page Link: | Join Here |
Conclusion:
In conclusion, our curated resource of TCS coding questions for 2023-2024, covering TCS Ninja to Digital Coding, provides a comprehensive preparation package for the upcoming TCS NQT exam. Offering not only TCS previous year coding questions but also detailed answers, our platform is designed to serve as a singular solution for aspirants. From TCS digital previous year coding questions to insights into the coding sheet structure for TCS NQT 2024, we aim to equip candidates with the necessary skills for success in the competitive landscape. Prepare strategically, master TCS NQT coding questions, and elevate your performance on exam day with our invaluable resource.