TCS Coding Questions with Solutions | FACE Prep

TCS Coding Questions with Solutions | FACE Prep

TCS Coding Questions & Solutions | FACE Prep

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.


TCS Coding Test: Important Guidelines

Before we dive into the coding questions, it’s essential to understand the instructions and format of the coding test:

  1. Time Limit: You will be given 1 coding question with a time limit of 20 minutes.
  2. Language Options: You can choose from the following programming languages:
    • C
    • C++
    • Java (Class name should be “Maze”)
    • Perl
    • Python 2.7
  3. IDE: You will be provided with an IDE (Integrated Development Environment) to write and debug your code.
  4. Input/Output: Your program should read input via STDIN (Standard Input) or Command Line Arguments, as specified in the question, and output should be written to STDOUT (Standard Output).

Key Points to Note:

  • No extra characters should be printed other than the required output.
  • If the output is a number, there should be no leading sign, unless it’s negative.
  • For floating-point numbers, ensure you maintain the required number of decimal places and avoid scientific notation (e.g., 3.9265E+2).

TCS Coding Questions with Solutions

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.

Question 1: Sum of Digits of a Number

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))

Question 2: Reverse a String

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))

Question 3: Check for Prime Number

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))

Question 4: Fibonacci Series (Up to N terms)

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)

Question 5: Factorial of a Number

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))

TCS Coding Question 1: Fibonacci & Prime Series

Problem Statement:
Consider the series:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, …
This series combines two sequences:

  • Odd positions follow the Fibonacci sequence.
  • Even positions follow prime numbers in ascending order.

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

TCS Coding Question 2: Geometric Series

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:

  • Odd-indexed terms follow the first geometric series: a1=1,r=2a_1 = 1, r = 2a1​=1,r=2
  • Even-indexed terms follow the second geometric series: a1=1,r=3a_1 = 1, r = 3a1​=1,r=3

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

TCS Coding Question 3: Even and Halved Sequence

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:

  • Odd-indexed terms are even numbers.
  • Even-indexed terms are derived by halving the previous odd-indexed term.

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

TCS Coding Question 4: String Manipulation

Problem Statement:
Given 3 strings as input, perform the following transformations:

  1. In the 1st string, replace vowels with #.
  2. In the 2nd string, replace consonants with *.
  3. In the 3rd string, convert lowercase letters to uppercase.

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()

Conclusion

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.

TCS Coding Questions & Solutions | FACE Prep