
Tcs nqt coding questions and answers
Tata Consultancy Services (TCS) is one of the top IT services companies in India. They conduct recruitment drives with a mix of multiple-choice questions (MCQs) and coding tasks. Below is an overview of their question pattern
salahuddin SK
Author
TCS Coding Interview Questions - Preparation Guide
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.
About TCS and Their Question Pattern
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) |
TCS Coding Questions and Solutions
Below are 20 important coding questions frequently asked in TCS recruitment exams, along with their solutions provided in Python:
1. Reverse a String 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"
2. Check if a String is a Palindrome
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
3. Find the Factorial of a Number
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
4. Implement a Binary Search Algorithm
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
5. Implement a Bubble Sort Algorithm
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]
6. Find the Maximum Element in an Array
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
7. Calculate Fibonacci Sequence up to a Certain Number
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]
8. Remove Duplicates from a List in Python
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]
9. Check if a Number is Prime in Python
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
10. Find the Sum of Digits of a Number in Python
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
11. Implement a Stack in Python
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
12. Implement a Queue in Python
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
13. Find the Intersection of Two Arrays in Python
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]
14. Count the Occurrences of Each Word in a Sentence in Python
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}
15. Implement a Linked List in Python
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
16. Find the Missing Number in an Array Containing 1 to N in Python
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
17. Check if Two Strings are Anagrams of Each Other in Python
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
18. Find the First Non-Repeating Character in a String in Python
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'
19. Rotate an Array to the Right by K Steps in Python
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]
20. Find the Intersection of Two Linked Lists in Python
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
Conclusion
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!
Comments (0)
Leave a Comment
About the Author
salahuddin SK
Sr. software Dev.
Passionate about sharing knowledge and helping developers learn new technologies.
Stay Updated
Get the latest articles and tutorials directly in your inbox.