Preparing for the TCS exam requires a clear understanding of the coding questions you may face, as they play a vital role in the selection process. This article will provide you with an overview of some commonly asked TCS coding questions, complete with solutions, and practical tips for succeeding in the coding round. Let’s dive into the specifics and get you ready for your upcoming TCS exam.
Before we dive into the coding questions, it’s essential to understand the instructions and format of the coding test:
3.9265E+2
).Here’s a look at some of the commonly asked coding questions in TCS recruitment drives. These problems typically involve patterns, series, and basic programming logic.
Write a program to find the sum of digits of a given number.
Input:987
Output:24
Solution (Python):
def sum_of_digits(n):
return sum(int(digit) for digit in str(n))
# Example usage
n = int(input("Enter a number: "))
print(sum_of_digits(n))
Write a program to reverse a given string.
Input:FACEPREP
Output:P R E P C A F
Solution (Python):
def reverse_string(s):
return " ".join(reversed(s))
# Example usage
s = input("Enter a string: ")
print(reverse_string(s))
Write a program to check whether a given number is prime or not.
Input:29
Output:Prime
Solution (Python):
def is_prime(n):
if n < 2:
return "Not Prime"
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return "Not Prime"
return "Prime"
# Example usage
n = int(input("Enter a number: "))
print(is_prime(n))
Write a program to print the first N Fibonacci numbers.
Input:6
Output:0 1 1 2 3 5
Solution (Python):
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
# Example usage
n = int(input("Enter the number of terms: "))
fibonacci(n)
Write a program to find the factorial of a given number.
Input:5
Output:120
Solution (Python):
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
# Example usage
n = int(input("Enter a number: "))
print(factorial(n))
Problem Statement:
Consider the series:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, …
This series combines two sequences:
Write a program to find the Nth term in this series.
Solution Approach:
You need to generate Fibonacci numbers for odd indices and prime numbers for even indices. A function to check for prime numbers will be crucial here.
# Function to check prime number
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
# Function to generate the nth term
def nth_term(n):
fib, primes = [1, 1], [2]
count_fib, count_prime = 2, 2
for i in range(3, n + 1):
if i % 2 != 0:
# Odd positions, Fibonacci numbers
fib.append(fib[count_fib-1] + fib[count_fib-2])
count_fib += 1
return fib[n//2]
else:
# Even positions, Prime numbers
while not is_prime(count_prime):
count_prime += 1
primes.append(count_prime)
count_prime += 1
return primes[n//2]
# Test with an example
print(nth_term(10)) # Output: 11
Problem Statement:
Consider the series:
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729, 128, 2187
This series combines two geometric sequences:
Write a program to find the Nth term in the series.
Solution Approach:
Generate two geometric progressions and return the corresponding term based on whether NNN is odd or even.
def nth_term_geometric(n):
# Odd terms form a geometric sequence with ratio 2
if n % 2 != 0:
return 2 ** (n // 2)
# Even terms form a geometric sequence with ratio 3
else:
return 3 ** (n // 2 - 1)
# Test with an example
print(nth_term_geometric(16)) # Output: 2187
Problem Statement:
Consider the series:
0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8
This series combines two sequences:
Write a program to find the Nth term in this series.
Solution Approach:
Maintain a counter for even numbers and halve previous terms for the even indices.
def nth_term_halved_sequence(n):
if n % 2 != 0:
return (n - 1) * 2 # Odd positions, even numbers
else:
return (n - 1) // 2 # Even positions, halved previous term
# Test with an example
print(nth_term_halved_sequence(10)) # Output: 4
Problem Statement:
Given 3 strings as input, perform the following transformations:
#
.*
.Solution Approach:
You need to write simple string manipulations, replacing characters as per the requirements.
def string_manipulation():
s1 = input() # Get first string
s2 = input() # Get second string
s3 = input() # Get third string
# Replace vowels with #
s1 = ''.join(['#' if ch in 'aeiouAEIOU' else ch for ch in s1])
# Replace consonants with *
s2 = ''.join(['*' if ch.isalpha() and ch not in 'aeiouAEIOU' else ch for ch in s2])
# Convert lowercase to uppercase
s3 = s3.upper()
print(s1)
print(s2)
print(s3)
# Example to run the function
string_manipulation()
To excel in the TCS Coding Round, it’s essential to understand the patterns behind the questions and practice solving them efficiently. The questions often focus on fundamental algorithms, series manipulations, and basic programming logic.