Tata Consultancy Services (TCS) is one of the leading IT services companies in India. Their recruitment drives are known for a balanced mix of multiple-choice questions (MCQs) and coding problems that assess candidates' problem-solving abilities and technical knowledge. This guide provides an overview of TCS's coding question patterns and presents 20 essential coding problems with Python solutions to help you prepare effectively.
TCS recruitment exams typically consist of 10 coding problems that cover a range of topics including Data Structures, Algorithms, and Strings. These questions vary in difficulty from beginner to hard and are designed to be solved within a 90-minute timeframe, although this may vary.
Topic | Details |
---|---|
Number of Questions | 10 Coding Problems |
Topics Covered | Data Structures, Algorithms, Strings |
Difficulty Level | Beginner to Hard |
Time Duration | 90 minutes (may vary) |
Below are 20 important coding questions frequently asked in TCS recruitment exams, along with their solutions provided in Python:
This question tests your ability to manipulate strings efficiently.
def reverse_string(s):
return s[::-1]
# Example usage:
print(reverse_string("hello")) # Output: "olleh"
Determine whether a given string reads the same forwards and backwards.
def is_palindrome(s):
return s == s[::-1]
# Example usage:
print(is_palindrome("racecar")) # Output: True
Calculate the factorial of a given non-negative integer using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
# Example usage:
print(factorial(5)) # Output: 120
Search for a target value within a sorted array efficiently.
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
# Example usage:
print(binary_search([1, 2, 3, 4, 5], 3)) # Output: 2
Sort an array of numbers using the Bubble Sort technique.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
# Example usage:
print(bubble_sort([5, 3, 2, 4, 1])) # Output: [1, 2, 3, 4, 5]
Identify the largest number in a given array.
def find_max(arr):
max_element = arr[0]
for num in arr:
if num > max_element:
max_element = num
return max_element
# Example usage:
print(find_max([5, 3, 9, 1, 7])) # Output: 9
Generate the Fibonacci sequence up to a specified limit.
def fibonacci(n):
fib_sequence = [0, 1]
while fib_sequence[-1] < n:
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
return fib_sequence[:-1]
# Example usage:
print(fibonacci(50)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Eliminate duplicate elements from a list, preserving unique values.
def remove_duplicates(arr):
return list(set(arr))
# Example usage:
print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) # Output: [1, 2, 3, 4, 5]
Determine whether a given number is a prime number.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Example usage:
print(is_prime(17)) # Output: True
Calculate the sum of all digits in a given integer.
def sum_of_digits(n):
return sum(int(digit) for digit in str(n))
# Example usage:
print(sum_of_digits(12345)) # Output: 15
Create a stack data structure with basic operations like push, pop, and peek.
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
def peek(self):
if not self.is_empty():
return self.items[-1]
def size(self):
return len(self.items)
# Example usage:
stack = Stack()
stack.push(1)
stack.push(2)
print(stack.peek()) # Output: 2
stack.pop()
print(stack.peek()) # Output: 1
Create a queue data structure with basic operations like enqueue, dequeue, and size.
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
# Example usage:
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
print(queue.dequeue()) # Output: 1
Identify common elements between two arrays.
def intersection(arr1, arr2):
return list(set(arr1) & set(arr2))
# Example usage:
print(intersection([1, 2, 3, 4], [3, 4, 5, 6])) # Output: [3, 4]
Count how many times each word appears in a given sentence.
def word_count(sentence):
words = sentence.split()
word_count_dict = {}
for word in words:
if word in word_count_dict:
word_count_dict[word] += 1
else:
word_count_dict[word] = 1
return word_count_dict
# Example usage:
print(word_count("the quick brown fox jumps over the lazy dog"))
# Output: {'the': 2, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1}
Create a singly linked list with basic operations such as insertion and traversal.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def traverse(self):
current = self.head
while current:
print(current.data, end=' ')
current = current.next
# Example usage:
ll = LinkedList()
ll.insert(3)
ll.insert(2)
ll.insert(1)
ll.traverse() # Output: 1 2 3
Identify the missing number in a sequence from 1 to N.
def find_missing_number(nums):
n = len(nums) + 1
total_sum = n * (n + 1) // 2
actual_sum = sum(nums)
return total_sum - actual_sum
# Example usage:
print(find_missing_number([1, 2, 4, 5])) # Output: 3
Determine whether two strings are anagrams, meaning they contain the same characters in a different order.
def are_anagrams(s1, s2):
return sorted(s1) == sorted(s2)
# Example usage:
print(are_anagrams("listen", "silent")) # Output: True
Identify the first character in a string that does not repeat.
def first_non_repeating_char(s):
char_count = {}
for char in s:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
for char in s:
if char_count[char] == 1:
return char
return None
# Example usage:
print(first_non_repeating_char("hello")) # Output: 'h'
Rotate the elements of an array to the right by K steps.
def rotate_array(nums, k):
k %= len(nums)
nums[:] = nums[-k:] + nums[:-k]
# Example usage:
arr = [1, 2, 3, 4, 5]
rotate_array(arr, 2)
print(arr) # Output: [4, 5, 1, 2, 3]
Determine the node at which two singly linked lists intersect.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def get_intersection_node(headA, headB):
pointerA, pointerB = headA, headB
while pointerA != pointerB:
pointerA = pointerA.next if pointerA else headB
pointerB = pointerB.next if pointerB else headA
return pointerA
# Example usage:
# Construct linked lists
intersect_node = ListNode(3)
intersect_node.next = ListNode(4)
headA = ListNode(1)
headA.next = ListNode(2)
headA.next.next = intersect_node
headB = ListNode(5)
headB.next = ListNode(6)
headB.next.next = intersect_node
intersection = get_intersection_node(headA, headB)
print(intersection.val) # Output: 3
Preparing for TCS coding interviews requires a solid understanding of fundamental programming concepts and problem-solving skills. By practicing these 20 coding problems, you'll be well-equipped to tackle the challenges presented in TCS's recruitment exams. Remember to not only understand the solutions but also to practice implementing them on your own to reinforce your learning. Good luck with your preparation!