Explore
ProGrad Programs
Placement Prep
TCS Codevita
Webinars
About Us

Edit
Reply




Edit

TCS NQT coding questions and answers for TCS NQT Coding test | Face Prep

Published on 11 Mar 2020

The coding question asked in slot 1 of the exam was of this sorts.


1) One programming language has the following keywords that cannot be used as identifiers - break, case, continue, default, defer else, for, func, goto, if, return, struct, type. Now write a program to check if the given word is a keyword or not.

TCS NQT coding questions are a must to prepare to clear the TCS NQT test. We are going to discuss all the previously asked TCS Ninja coding questions in this article. These will help you prepare for any kind of coding questions asked in the exam


TCS NQT Coding questions - Important instructions


Instructions


1) Only One question, 20 minutes.

2) Choice of C / C++ / Java / Perl / Python 2.7.

3) Provided an IDE to debug.

4) For Java, the class name should be named Maze.

5)Input to the program either through STDIN / Command line arguments, as per the instructions.

6)Program should write the output to STDOUT.

7) Public and private test cases based evaluation.


Points to note


1) While printing the output no leading or trailing spaces should be printed.

2) Other than the required output, no other text should be printed.

3) If the output is a number, no leading sign must be printed unless it is a negative number.

4) No scientific notation (3.9265E + 2).

5) All floating point numbers must contain that many decimal places as mentioned in the question.


TCS Ninja Coding Questions 2019 with solutions


Note: The below given questions were asked in TCS Ninja National Qualifier test 2018.


TCS Ninja coding questions - 1


Consider the below series:


1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, ...


This series is a mixture of 2 series - all the odd terms in this series form a Fibonacci series and all the even terms are the prime numbers in ascending order.


Write a program to find the Nth term in this series.


The value N is a Positive integer that should be read from STDIN. The Nth term that is calculated by the program should be written to STDOUT. Other than the value of Nth term, no other characters/strings or message should be written to STDOUT.


For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to STDOUT.


Solution in C

#include<stdio.h>
#define MAX 1000
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("%d", t1);
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
if(++count == n)
{
printf("%d", i);
break;
}
}
}
int main()
{
int n;
scanf("%d", &n);
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}


Solution in C++

#include<iostream>
using namespace std;
#define MAX 1000

void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
cout << t1;
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
if(++count == n)
{
cout << i;
break;
}
}
}
int main()
{
int n;
cin >> n;
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}


Output:

Input: 14
Output: 17


TCS Ninja coding questions - 2


Given a series whose even term creates a separate geometric series and odd term creates another geometric series.


Write a program to generate such series.For example,1, 1, 2, 2, 4, 4, 8, 8, 16, 16,......


Solution in C

#include

int main()
{
int n, i, r1, r2;
printf("nEnter the total number of terms : ");
scanf("%d", &n);
printf("nEnter the common ratio for GP - 1 : ");
scanf("%d", &r1);
printf("nEnter the common ratio for GP - 2 : ");
scanf("%d", &r2);
printf("nThe series isn");
int a = 1, b = 1;
if(n % 2 == 0)
{
for(i = 0; i < n/2; i++)
{
printf("%d ", a);
a = a * r1;
printf("%d ", b);
b = b * r2;
}
}
else
{
for(i = 0; i < n/2; i++)
{
printf("%d ", a);
a = a * r1;
printf("%d ", b);
b = b * r2;
}
printf("%d ", a);
}
printf("n");

}


Solution in C++

#include<iostream>
using namespace std;

int main()
{
int n, i, r1, r2;
cout << "nEnter the total number of terms : ";
cin >> n;
cout << "nEnter the common ratio for GP - 1 : ";
cin >> r1;
cout << "nEnter the common ratio for GP - 2 : ";
cin >> r2;
cout << "nThe series isn";
int a = 1, b = 1;
if(n % 2 == 0)
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;
}
}
else
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;;
}
cout << a << " ";
}
cout << endl;
}


Input:

Enter the number of terms : 10
Enter the common ratio for G.P - 1 : 2
Enter the common ratio for G.P - 2 : 3

Output:

The series is
1 1 2 3 4 9 8 27 16 81


Most asked TCS Ninja Coding questions so far


TCS Ninja coding questions range from easy to medium difficulty level. TCS Ninja coding test questions generally get repeated from the same set of concepts and programs. Here are some of the most asked TCS Coding questions with solutions in all possible languages are given.


  1. Factorial of a number
  2. Area of a circle
  3. Leap year or not
  4. GCD of two number
  5. Check whether a given number is a prime number or not
  6. Print all prime numbers in a given range
  7. Check whether a given number is a Strong number or not
  8. Chcek whether a number is a Palindrome or not
  9. Check whether a string is a Palindrome or not
  10. Check whether a given number is an Armstrong number or not
  11. Print all Armstrong numbers between two intervals
  12. Fibonacci series generation upto N terms
  13. Binary to decimal conversion
  14. Decimal to binary conversion
  15. Decimal to octal conversion
  16. Octal to decimal conversion
  17. Binary to octal conversion
  18. Octal to binary conversion
  19. Sum of all prime numbers within a range
  20. Reversing a given number
  21. Reverse a string
  22. Pyramid pattern using stars
  23. Pyramid pattern using numbers
  24. Diamond pattern printing using stars
  25. Diamond pattern printing using numbers
  26. Second smallest element in an array
  27. Remove duplicate elements in an array


Practice more TCS Ninja coding questions

If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in