TCS NQT Menu9>
- Placement Papers
- What is TCS NQT?
- How To Apply
- Foundation Section
- Aptitude Questions
- English Verbal
- Reasoning Ability
- Advanced Section
- Advanced Quantitative Ability
- Advanced Reasoning Ability
- Advanced Coding
- Coding Questions
- Syllabus 2024
- Recruitment Process
- Registration Process
- Eligibility Criteria
- How to prepare for TCS NQT?
- Interview Questions
- Hiring Process
PREPINSTA PRIME
TCS Advance Coding Questions
TCS Advance Coding Questions with Solutions
TCS has announced the hiring for 2024 passouts. Through this new hiring pattern you can get placed in Ninja or Digital depending upon your performance in the recruitement test. TCS has an advanced coding section in this year hiring pattern, that will help you in clearing the role for Digital Profile.
Here, in this article you can practice previous year sample based coding questions, for TCS Advanced Coding Round.
Lets Prepare Better Faster
Question 1
Problem Statement-:
Find the minimum number of coins required to form any value between 1 to N,both inclusive.Cumulative value of coins should not exceed N. Coin denominations are 1 Rupee, 2 Rupee and 5 Rupee.Let’s Understand the problem using the following example. Consider the value of N is 13, then the minimum number of coins required to formulate any value between 1 and 13, is 6. One 5 Rupee, three 2 Rupee and two 1 Rupee coins are required to realize any value between 1 and 13. Hence this is the answer. However, if one takes two 5 Rupee coins, one 2 rupee coin and two 1 rupee coin, then too all values between 1 and 13 are achieved. But since the cumulative value of all coins equals 14, i.e., exceeds 13, this is not the answer.
Input Format:
- A single integer value.
Output Format:
- Four space separated integer values.
- 1st – Total number of coins.
- 2nd – number of 5 Rupee coins.
- 3rd – number of 2 Rupee coins.
- 4th – number of 1 Rupee coins.
Constraints:
- 0 < n < 1000
Refer the sample output for formatting
Sample Input
13
Sample Output
6 1 3 2
Explanation
- The minimum number of coins required is 6 with in it:
- minimum number of 5 Rupee coins = 1
- minimum number of 2 Rupee coins = 3
- minimum number of 1 Rupee coins = 2
- Using these coins, we can form any value with in the given value and itself, like below:
- Here the given value is 13
- For 1 = one 1 Rupee coin
- For 2 = one 2 Rupee coin
- For 3 = one 1 Rupee coin and one 2 Rupee coins
- For 4 = two 2 Rupee coins
- For 5 = one 5 Rupee coin
- For 6 = one 5 Rupee and one 1 Rupee coins
- For 7 = one 5 Rupee and one 2 Rupee coins
- For 8 = one 5 Rupee, one 2 Rupee and one 1 Rupee coins
- For 9 = one 5 Rupee and two 2 Rupee coins
- For 10 = one 5 Rupee, two 2 Rupee and one 1 Rupee coins
- For 11 = one 5 Rupee, two 2 Rupee and two 1 Rupee coins
- For 12 = one 5 Rupee, three 2 Rupee and one 1 Rupee coins
- For 13 = one 5 Rupee, three 2 Rupee and two 1 Rupee coins
import java.util.*; public class Main { public static void main(String[] args) { System.out.println("Enter the Number"); Scanner s = new Scanner(System.in); int number = s.nextInt(); int one = 0,two = 0; int five = (number-4)/5; if(((number-5*five) % 2) == 0){ one = 2; } else{ one = 1; } two = (number-5*five-one)/2; System.out.println(one+two+five); System.out.println(five); System.out.println(two); System.out.println(one); } }
number = int(input()) if(number ==0): print(0,0,0,0) else: five = int((number-4)/5) if((number-5*five) % 2) == 0: one=2 else: one=1 two=(number-5*five-one)//2 print(one+two+five,five,two,one)
#includeusing namespace std; int main() { int n,total,five,two,one; cin>>n; if(!n) { cout << 0 << " " << 0 << " " << 0 << " " << 0 << endl; } int t=n/5; int tr=n%5; if(n>3 && tr<4) { tr+=5; t-=1; } int dp[10][3]={{0,0,0},{1,0,0},{2,0,0},{1,1,0},{2,1,0},{1,2,0},{2,2,0},{1,3,0},{2,3,0},{2,1,1}}; total=t+ dp[tr][0]+dp[tr][1]; cout << total << " " << t << " "<< dp[tr][1] << " " << dp[tr][0] << endl; }
Question 2
Problem Statement-:
The problem solvers have found a new Island for coding and named it as Philaland. These smart people were given a task to make purchase of items at the Island easier by distributing various coins with different values. Manish has come up with a solution that if we make coin categories starting from $1 till the maximum price of the item present on Island, then we can purchase any item easily. He added the following example to prove his point.
Let’s suppose the maximum price of an item is 5$ then we can make coins of {$1, $2, $3, $4, $5}to purchase any item ranging from $1 till $5.
Now Manisha, being a keen observer, suggested that we could actually minimize the number of coins required and gave the following distribution {$1, $2, $3}. According to him any item can be purchased one time ranging from $1 to $5. Everyone was impressed with both of them.Your task is to help Manisha come up with a minimum number of denominations for any arbitrary max price in Philaland.
Input Format
- First line contains an integer T denoting the number of test cases.
- Next T lines contain an integer N denoting the maximum price of the item present Philaland.
Output Format
- For each test case print a single line denoting the minimum number of denominations of coins required.
Constraints
- 1<=T<=100
- 1<=N<=5000
Refer the Sample Output Formatting
Sample Input:
2
10
5
Sample Output:
4
3
Explanation:
For test case 1, N=10.
According to Manish {$1, $2, $3,… $10} must be distributed.
But as per Manisha only {$1, $2, $3, $4} coins are enough to purchase any item ranging from $1 to $10. Hence the minimum is 4. Likewise denominations could also be {$1, $2, $3, $5}. Hence the answer is still 4.
For test case 2, N=5.
According to Manish {$1, $2, $3, $4, $5} must be distributed.
But as per Manisha only {$1, $2, $3} coins are enough to purchase any item ranging from $1 to $5. Hence minimum is 3. Likewise denominations could also be {$1, $2, $4}. Hence the answer is still 3.
#include <stdio.h> int sum(int x[],int l) { int j,s1=0; for(j=1;j<=l;j++) s1=s1+x[j]; return s1; } int main() { int n,a[100],k=1,i,c,t,j; scanf("%d",&t); for(j=0;jsum(a,k)) { k++; a[i]=k; } } for(i=1;i<=k;i++) { if(a[i]!=0) c++; } printf("%d\n",c); for(i=1;i<=k;i++) a[i]=0; k=1; } return 0; }
#include <bits/stdc++.h> using namespace std; int getMinDeno(int n) { return log2(n) + 1; } // Driver Code int main() { int t,n; vector res; cin >> t; while(t--){ cin >> n; res.push_back(getMinDeno(n)); } for(int i: res) cout << i <<endl; return 0; }
import java.util.*; import java.lang.reflect.Array; class Main{ public static void main(String[] args) { System.out.println("Enter number of ways :"); Scanner sc= new Scanner(System.in); int no_of_ways = sc.nextInt(); int [] allinone = new int[no_of_ways]; for(int j=0;j < no_of_ways;j++){ int count = 0; int values = (int)Array.get(allinone, j); while(values >= 1){ values = values / 2; count+=1; } System.out.println(count); } } }
no_of_ways=int(input()) for i in range(1,no_of_ways+1): value=int(input()) count = 0 while value>=1: value=value//2 count=count+1 print(count)
Question 3
Problem Statement-:
Given a number ‘N’,find all possible divisors and print them in increasing order.
Input Format:
- The first line of input contains an integer ‘N’ denoting the number.
Output Format:
- Print a line containing all the divisors in increasing order separated by space.
Constraints:
- 1 <= N <= 10^8
Refer the sample output for formatting
Sample Input:
10
Sample Output:
1 2 5 10
// Aux Space Complexity: O(sqrt(n)) // Time complexity : O(sqrt(n)) #include <bits/stdc++.h> using namespace std; void divineDivisors(int n) { // Storing other half after sqrt(n) vectorres; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { // check if divisors are equal if (n / i == i) cout << i; else { cout << i << " "; // push pair in vector res.push_back(n / i); } } } vector ::iterator it; // The vector will be printed in reverse for (it = res.end() - 1; it != res.begin(); --it) cout << ' ' << *it; cout << ' ' << n; } // Driver Code int main() { int n; cin >> n; divineDivisors(n); return 0; }
N = int(input('Enter the number :')) arr = [] for i in range(1,N+1): if N % i == 0: arr.append(i) sorted(arr) print(arr)
Question 4
Problem Statement:-
In a Conference ,attendees are invited for dinner after the conference. The Co-ordinator,Sagar arranged around round tables for dinner and wanted to have an impactful seating experience for the attendees. Before finalising the seating arrangement, he wants to analyse all the possible arrangements.
These are R round tables and N attendees.In case where N is an exact multiple of R, the number of attendees must be exactly N/R.
If N is not an exact multiple of R, then the distribution of attendees must be as equal as possible.Please refer to the example section before for better understanding.
For example, R = 2 and N = 3
All possible seating arrangements are
(1,2) & (3)
(1,3) & (2)
(2,3) & (1)
Attendees are numbered from 1 to N.
Constraints:
- 0 <= R <= 10(Integer)
- 0 < N <= 20 (Integer)
Input Format:
- The first line contains T denoting the number of test cases.
- Each test case contains two space separated integers R and N, Where R denotes the number of
- round tables and N denotes the number of attendees.
Output Format:
- Single Integer S denotes the number of possible unique arrangements.
Sample Input:
Input:
1
2 5
Output:
10
Explanation:
R = 2, N = 5
(1,2,3) & (4,5)
(1,2,4) & (3,5)
(1,2,5) & (3,4)
(1,3,4) & (2,5)
(1,3,5) & (2,4)
(1,4,5) & (2,3)
(2,3,4) & (1,5)
(2,3,5) & (1,4)
(2,4,5) & (1,3)
(3,4,5) & (1,2)
Arrangements like
(1,2,3) & (4,5)
(2,1,3) & (4,5)
(2,3,1) & (4,5) etc.
But as it is a round table,all the above arrangements are the same.
import java.util.*; class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int testcases = scanner.nextInt(); int tables = 0,people; while (testcases-->0) tables = scanner.nextInt(); people = scanner.nextInt(); System.out.println(find(tables,people)); } public static int find(int r,int n) { int x = n/r; int y1 = n%r; int x1 = 0; int ans1 = 1; while(r!=0) { if(y1>0) { x1 = x+1; y1 = y1-1; } else { x1 = x; } ans1 = ans1*combination(n,x1); n = n-x1; r--; } return ans1; } public static int factorial(int n) { if(n==0||n==1) { return 1; } return n * factorial(n-1); } public static int combination(int n,int r) { return factorial(n)/(factorial(n-r)*factorial(r)); } }
testcases = int(input()) for i in range(testcases): tables, people = map(int, input().split()) result = 1 if tables >= people: print(result) else: PA = people // tables PB = PA + 1 TB = people % tables TA = tables - TB # Using DP to store factorials pre-hand fact = [1] for j in range(1, people + 1): fact.append(j*fact[j-1]) for i in range(TB): result = result * (fact[people]//(fact[PB]*fact[people-PB])) people = people - PB for i in range(TA): result = result * (fact[people]//(fact[PA]*fact[people-PA])) people = people - PA print(result)
#include<bits/stdc++.h> using namespace std; typedef long long int ll; map <ll,ll> dp; int fac(int n) { if(dp[n]) return dp[n]; dp[n]=n*fac(n-1); return dp[n]; } int func(int n) { if(n <= 3) return 1; return fac(n-1); } int main() { dp[0]=dp[1]=1; int tests;cin >> tests; while(tests--) {int R,N,c=1; cin >> R >> N; if(N <= R) { cout << 1; continue; } int a=N/R,n=N%R; ll ans=fac(N)/(pow(fac(a),R-n) * pow(fac(a+1),n) )/fac(n)/fac(R-n); for(int i=1;i <=n ;i++) c*=func(a); for(int i=n+1;i <= R;i++) c*=func(a-1); cout << c*ans;} }
Question 5
Problem Statement:-
Compute the nearest larger number by interchanging its digits updated.Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1.
Constraints:
- 1 <= a,b <= 10000000
Input Format:
- 2 numbers a and b, separated by space.
Output Format:
- A single number greater than b.
- If not possible, print -1
Example 1:
Input:
459 500
Output:
549
Example 2:
Input:
645757 457765
Output:
465577
import sys a=input().strip() b=input().strip() a=''.join(sorted(a)[::-1]) if int(a)<=int(b): print(-1) sys.exit() else: if len(a)>len(b): print(int(a[::-1])) sys.exit() else: n=len(b) a=list(a[::-1]) s='' n1=len(a) for i in range(n): d=b[i] j=0 while a[j]b[i]: s+=a[j] a.remove(a[j]) a.sort() s+=''.join(a) print(int(s)) sys.exit() else: s1=s+a[j] g=a.copy() g.remove(a[j]) if n1>1: g.sort() g=g[::-1] s1+=''.join(g) if int(s1)>int(b): s+=a[j] a.remove(a[j]) n1-=1 else: while a[j]<=b[i]: j+=1 s+=a[j] a.remove(a[j]) a.sort() s+=''.join(a) print(int(s)) sys.exit() print(int(s))
#include<bits/stdc++.h> using namespace std; int main() { string a; int b,c; cin>>a>>b; sort(a.begin(),a.end(),greater()); c=atoi(a.c_str()); if(b>c) {cout<<-1; return 0;} while(b
Question 6
Problem Statement:-
In this Even-Odd problem – We are given a range [low, high] (both inclusive), and an integer K such that we have to select K numbers from the range (a number can be chosen multiple times) such that the 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.
#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
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)
Question 7
Problem Statement:-
Three characters { #, *, . } represents a constellation of stars and galaxies in space. Each galaxy is demarcated by # characters. There can be one or many stars in a given galaxy. Stars can only be in the shape of vowels { A, E, I, O, U }. A collection of * in the shape of the vowels is a star. A star is contained in a 3×3 block. Stars cannot be overlapping. The dot(.) character denotes empty space.
Given 3xN matrix comprising of { #, *, . } character, find the galaxy and stars within them.
Note: Please pay attention to how vowel A is denoted in a 3×3 block in the examples section below.
Constraints
3 <= N <= 10^5
Input
Input consists of a single integer N denoting the number of columns.
Output
The output contains vowels (stars) in order of their occurrence within the given galaxy. The galaxy itself is represented by the # character.
Example 1
Input
18
* . * # * * * # * * * # * * * . * .
* . * # * . * # . * . # * * * * * *
* * * # * * * # * * * # * * * * . *
Output
U#O#I#EA
Explanation
As it can be seen that the stars make the image of the alphabets U, O, I, E, and A respectively.
Example 2
Input
12
* . * # . * * * # . * .
* . * # . . * . # * * *
* * * # . * * * # * . *
Output
U#I#A
Explanation
As it can be seen that the stars make the image of the alphabet U, I, and A.
n=int(input()) co=[] for i in range(3):co.append(list(input().split())) i=0 while i < n-2: if co[0][i]=='#': print("#",end="") i+=1 continue if co[0][i]=='.' and co[0][i+1]=='*' and co[0][i+2]=='.': if co[1][i]=='*' and co[1][i+1]=='*' and co[1][i+2]=='*': if co[2][i]=='*' and co[2][i+1]=='.' and co[2][i+2]=='*': print('A',end="") if co[0][i]=='*' and co[0][i+1]=='*' and co[0][i+2]=='*': if co[1][i]=='*' and co[1][i+1]=='*' and co[1][i+2]=='*': if co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*': print('E',end="") i+=3 continue elif co[1][i]=='.' and co[1][i+1]=='*' and co[1][i+2]=='.': if co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*': print('I',end="") i+=3 continue elif co[1][i]=='*' and co[1][i+1]=='.' and co[1][i+2]=='*': if co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*': print('O',end="") i+=3 continue if co[0][i]=='*' and co[0][i+1]=='.' and co[0][i+2]=='*': if co[1][i]=='*' and co[1][i+1]=='.' and co[1][i+2]=='*': if co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*': print('U',end="") i+=3 continue i+=1
#include<bits/stdc++.h> using namespace std; int main() { int n;cin>>n; char co[3][n]; for(int i=0;i<3;i++) { for(int j=0;j<n;j++) cin>>co[i][j]; } for(int i=0;i<n-2;i++) { if(co[0][i]=='#') {cout<<"#";continue;} if(co[0][i]=='.' && co[0][i+1]=='*' && co[0][i+2]=='.') { if(co[1][i]=='*' && co[1][i+1]=='*' && co[1][i+2]=='*') if(co[2][i]=='*' and co[2][i+1]=='.' and co[2][i+2]=='*') cout<<"A";i+=2;continue; } if(co[0][i]=='*' and co[0][i+1]=='*' and co[0][i+2]=='*') { if (co[1][i]=='*' and co[1][i+1]=='*' and co[1][i+2]=='*') { if (co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*') {cout<<"E";i+=2;continue;} } else if(co[1][i]=='.' and co[1][i+1]=='*' and co[1][i+2]=='.') { if (co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*') {cout<<"I";i+=2;continue;} } else if(co[1][i]=='*' and co[1][i+1]=='.' and co[1][i+2]=='*') { if(co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*') {cout<<"O";i+=2;continue;} } } if(co[0][i]=='*' and co[0][i+1]=='.' and co[0][i+2]=='*') if(co[1][i]=='*' and co[1][i+1]=='.' and co[1][i+2]=='*') if(co[2][i]=='*' and co[2][i+1]=='*' and co[2][i+2]=='*') { cout<<"U";i+=2; continue; } } }
Question 8
Problem Statement:-
Here on earth, our 24-hour day is composed of two parts, each of 12hours. Each hour in each part has a corresponding hour in the other parts separated by 12 hours: the hour essentially measures the duration since the start of the day part. For example, 1 hour in the first part of the day is equivalent to 13, which is 1 hour into the second part of the day.Now, consider the equivalent hours that are both prime numbers. We have 3 such instances for a 24-hour 2-part day:
5~17
7~19
11~23
Accept two natural numbers D, P >1 corresponding respectively to number
of hours per day and number of parts in a day separated by a space. D
should be divisible by P, meaning that the number of hours per part (D/P)
should be a natural number. Calculate the number of instances of
equivalent prime hours. Output zero if there is no such instance.
Note that we require each equivalent hour in each part in a day to be a prime number.
Example:
Input: 24 2
Output:3 (We have 3 instances of equivalent prime hours: 5~17, 7~19 and
11~23.)
Constraints:
10 <= D < 500
2 <= P < 50
Input:
Single line consists of two space separated integers, D and P
corresponding to number of hours per day and number of parts in a day
respectively.
Output:
Output must be a single number, corresponding to the number of
instances of equivalent prime number, as described above
Time Limit:
1
Examples
Example 1
Input
36 3
Output
2
Explanation
In the given test case D = 36 and P = 3
Duration of each day part = 12
2~14~X
3~15~X
5~17~29 – instance of equivalent prime hours
7~19~31 – instance of equivalent prime hours
11~23~X
Hence the answers is 2.
Example 2
Input
49 7
Output
0
Explanation
Duration of each day part = 7
2~9~X~23~X~37~X
3~X~17~X~31~X~X
5~X~19~X~X~X~47
7~X~X~X~X~X~X
Hence there are no equivalent prime hours.
def prime(n): if n == 1: return False for i in range(2, int(math.sqrt(n) + 1)): if n % i == 0: return False return True d, p = map(int, (input().split())) n = int(d / p) count = 0 # as 0, 1 not prime so starting from 2 for i in range(2, n): prime_time = True for j in range(p): num = i + j*n if not prime(num): prime_time = False break if prime_time: count += 1 print(count)
#include<bits/stdc++.h> using namespace std; bool isprime(int n) { if (n == 1) return false; for (int i = 2; i <= (int)sqrt(n); i++) { if (n % i == 0) return false; } return true; } int main() { int D, P, i, j, p, t = 1; cin >> D >> P; p = D / P; int time[p][P]; for (i = 0; i < P; i++) { for (j = 0; j < p; j++) { time[j][i] = t++; } } t = 0; for (i = 0; i < p; i++) { bool flag = true; for (j = 0; j < P; j++) { if (!isprime(time[i][j])) { flag = false; break; } } if (flag) t++; } cout << t; }
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
Login/Signup to comment