TCS Coding Questions

TCS Coding Questions

TCS Coding section mainly focuses on candidates basic ability to write a simple and clean code. Therefore they don’t ask very tough questions, mostly the question level is moderate and if you have a good grip on basic programming concepts you should be able to solve them very easily. 

For example, look at this question (TCS Ninja 2019 & TCS NQT 2020)

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.

Question breakdown

Just read the question once again, if you carefully observe, to solve the above problem you should know how to calculate the prime number and then Fibonacci numbers and even odd positions. So if you know these mentioned basic concepts then you can easily write the code for the above question. 

So, it highly recommended learning the basics thoroughly before solving the previous year questions.And if you have just started preparing for the coding and need some guidance on how to prepare for TCS NQT coding round then do watch this video . Here is a list of all the core concepts essential for TCS coding section

  1. Program to find if the given number is even & odd 
  2. Program to check if the given number is prime
  3. Program to check if the given number is a palindrome
  4. Program to check if the given number is perfect
  5. Program to check if the given number is Armstrong
  6. Program to check if the given number is Strong
  7. Program to count the digits in the given number
  8. Program to find the factorial of the given number
  9. Program to print Fibonacci series
  10. Program to swap two numbers 
  11. Program to print HelloWorld without a semicolon
  12. Program to find the largest of two numbers without conditional
  13. Program to find the area of a triangle when the sides are given
  14. Program to find the volume of a sphere 
  15. Program to find if the given year is leap year
  16. Program to convert decimal to binary and vice versa
  17. Program to convert binary to octal
  18. Program to find the LCM of two numbers 
  19. Program to find the GCD of two numbers
  20. Program to display different patterns
  21. Program to find the largest and smallest element in the array
  22. Program to find the second largest element in the array
  23. Program to remove duplicate numbers from an array
  24. Program to perform a linear search
  25. Writing command line argument programs

Assuming that you have mastered the fundamentals now the next important thing is to solve all the previous year tcs nqt questions. To make your learning process easy we are listing them here for your reference. Also do note that remembering the code doest make any sense because you may not see the exact same question. It has been observed that TCS generally make some minor modifications to the below-mentioned questions every year. So your focus should be learning the approach of solving the question rather than blind mug up.

The following questions have appeared in actual TCS exam in 2019 & 2020, the code solutions are written in python which is really easy to pick up even if you are from the non-cs background. Just in case you are not good at python then I would definitely recommend you to watch this python fundamentals series on youtube which is designed to help you master concepts relevant for interviews. 

Question 1


Given a series whose even term represents a geometric series with common ratio 3 and the odd term is another geometric series with common ratio 4

Write a program to print the Nth number in the series. Here N is sent as a command-line argument. 

For example 1, 1, 3, 4, 9,16, 27 ….

Approach

Geometric series is of format a, ar ar2, ar3…. Here a is the first number and r is the common ratio. From the problem statement, we know that a is 1 in both even and odd positions and common ratios are 3 and 4 respectively.  

The approach to solve this is when n is given, we find whether n is even or odd, based on that we can divide it by 2 since its a mix of two series and then find the appropriate number. Also, it’s important to note that when n is even division by 2 is an integer but when divided by the odd number we get a decimal value so we can round it off to higher-order i.e 3.5 is converted to 4

Code Solution

# Mixed Geometric series question
# Author - Krishna Teja
# website - packetprep.com
def checkEven(num):
    if num % 2 ==0:
        return 1
    else:
        return 0

def geometricSeries(a,r,pos):
    return int(a*(r**(pos-1)))

num = int(input())
if checkEven(num):
    print(geometricSeries(1,4,num//2))
else:
    print(geometricSeries(1,3,num//2+1))

Question 2


Write a program to print reverse substring of first four letters of the given input

eg input: Indianocean, output: idni

Approach

This could be solved by two steps, one to find the substring of the given word then reversing the substring

Code Solution

# Reverse Substring Question
# Author - Krishna Teja
# website - packetprep.com

str = input("Enter a string: ")
str_new = str[0:4]
print(str_new[::-1])

Question 3


Write a python program to add 10 subsequent prime numbers from the given prime number.

Approach

The easy way would be writing a loop with a counter for 10 and iterating to check if each number is prime or not if prime then we will add it to our temp variable.

Code Solution

# Subsequent Prime Numbers
# Author - Krishna Teja
# website - packetprep.com

def prime(num):
    for i in range(2,num):
        if num % i == 0:
            return 0
    return 1

num = int(input("Enter a prime number:"))
counter = 0
sum = 0
while(counter!=10):
    num = num + 1
    if prime(num):
        counter = counter + 1
        sum = sum + num
        # print(nusm, sum)

print(sum)

Question 4


Write a program to find the sum of cubes of natural numbers up to n.

Approach

Use a loop till n and do the summation of the cube of each number

Code Solution

# Cubes Summation
# Author - Krishna Teja
# website - packetprep.com

num = int(input("Enter one natural number: "))

sum = 0
for i in range(1,num+1):
    sum = sum + (i * i * i)

print("The sum of the cubes of the natural numbers is :",sum)

Question 5


write a program to print the given input string by removing vowels and padding zeros equivalent to the number of vowels

eg: input: apple, output: ppl00

Approach

To solve this we need a counter to keep a track of vowels and a loop for iterate on each character to check for vowels 

Code Solution

# String replacement
# Author - Krishna Teja
# website - packetprep.com

l = ['a','e','i','o','u']

str = input("Enter the string: ")
counter = 0
new_str = ''
for s in str:
    if s not in l:
        new_str =new_str+s
    else:
        counter= counter +1
for i in range(counter):
    new_str =new_str + '0'

print(new_str)