Instagram
youtube
Facebook
  • 21 hours ago
  • 33 Views

TCS Coding Questions 2024 – Essential Problems with Solutions

Dev Kanungo
Table of Contents

Get ready for TCS Coding Questions 2024 with a curated set of programming problems, covering fundamental to advanced concepts in Python. Perfect for TCS NQT and placement preparation.

Basic Level:

  1. Write a program to check if a given number is prime or not.

    def is_prime(n):
        if n < 2:
            return False
        for i in range(2, int(n ** 0.5) + 1):
            if n % i == 0:
                return False
        return True
    
    print(is_prime(17))  # Output: True
    
  2. Implement a program to find the GCD (Greatest Common Divisor) of two numbers.

    import math
    def find_gcd(a, b):
        return math.gcd(a, b)
    
    print(find_gcd(24, 36))  # Output: 12
    
  3. Write a program to reverse a given string without using built-in functions.

    def reverse_string(s):
        return s[::-1]
    
    print(reverse_string("hello"))  # Output: "olleh"
  4. Implement a function to check if a given string is a palindrome or not.

    def is_palindrome(s):
        return s == s[::-1]
    
    print(is_palindrome("racecar"))  # Output: True
    
  5. Write a program to find the sum of digits of a given number recursively.

    def sum_of_digits(n):
        if n == 0:
            return 0
        return n % 10 + sum_of_digits(n // 10)
    
    print(sum_of_digits(123))  # Output: 6
    

Intermediate Level:

  1. Given an array, write a program to find the second largest number in it.

    def second_largest(arr):
        unique_nums = list(set(arr))
        unique_nums.sort()
        return unique_nums[-2] if len(unique_nums) > 1 else None
    
    print(second_largest([10, 20, 4, 45, 99]))  # Output: 45
    

     

  2. Implement a function to find duplicate elements in an array.

    def find_duplicates(arr):
        seen, duplicates = set(), set()
        for num in arr:
            if num in seen:
                duplicates.add(num)
            seen.add(num)
        return list(duplicates)
    
    print(find_duplicates([1, 2, 3, 4, 5, 2, 3, 6]))  # Output: [2, 3]
    

     

  3. Given a string, write a program to count the occurrences of each character.

    from collections import Counter
    
    def char_count(s):
        return dict(Counter(s))
    
    print(char_count("hello"))  # Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
    

     

  4. Write a program to implement binary search on a sorted array.

    def binary_search(arr, target):
        left, right = 0, len(arr) - 1
        while left <= right:
            mid = (left + right) // 2
            if arr[mid] == target:
                return mid
            elif arr[mid] < target:
                left = mid + 1
            else:
                right = mid - 1
        return -1
    
    print(binary_search([1, 3, 5, 7, 9], 7))  # Output: 3
    

     

  5. Given a matrix, write a function to find the transpose of the matrix.

    def transpose(matrix):
        return list(map(list, zip(*matrix)))
    
    matrix = [[1, 2, 3], [4, 5, 6]]
    print(transpose(matrix))  # Output: [[1, 4], [2, 5], [3, 6]]
    

     


Advanced Level:

  1. Write a program to implement the Bubble Sort algorithm.

    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
    
    print(bubble_sort([64, 34, 25, 12, 22, 11, 90]))  # Sorted Output
    

     

  2. Implement a function to check if a given number is an Armstrong number.

    def is_armstrong(n):
        return n == sum(int(digit) ** len(str(n)) for digit in str(n))
    
    print(is_armstrong(153))  # Output: True
    

     

  3. Write a program to find the longest common prefix in an array of strings.

    def longest_common_prefix(strs):
        if not strs:
            return ""
        prefix = strs[0]
        for string in strs[1:]:
            while string[:len(prefix)] != prefix:
                prefix = prefix[:-1]
                if not prefix:
                    return ""
        return prefix
    
    print(longest_common_prefix(["flower", "flow", "flight"]))  # Output: "fl"
    

     

  4. Implement a function to find the missing number in an array of consecutive numbers.

    def missing_number(arr):
        n = len(arr) + 1
        return n * (n + 1) // 2 - sum(arr)
    
    print(missing_number([1, 2, 3, 5]))  # Output: 4
    

     

  5. Write a program to find the first non-repeating character in a given string.

    def first_non_repeating(s):
        counts = Counter(s)
        for char in s:
            if counts[char] == 1:
                return char
        return None
    
    print(first_non_repeating("swiss"))  # Output: "w"
    

     

Data Structure and Algorithm Focused:

  1. Write a program to implement a stack using an array or linked list.

    class Stack:
        def __init__(self):
            self.stack = []
        
        def push(self, val):
            self.stack.append(val)
        
        def pop(self):
            return self.stack.pop() if self.stack else None
    
    s = Stack()
    s.push(10)
    s.push(20)
    print(s.pop())  # Output: 20
    

     

  2. Implement a queue using two stacks.

    class Queue:
        def __init__(self):
            self.stack1 = []
            self.stack2 = []
    
        def enqueue(self, val):
            self.stack1.append(val)
    
        def dequeue(self):
            if not self.stack2:
                while self.stack1:
                    self.stack2.append(self.stack1.pop())
            return self.stack2.pop() if self.stack2 else None
    
    q = Queue()
    q.enqueue(1)
    q.enqueue(2)
    print(q.dequeue())  # Output: 1
    

     

  3. Given a linked list, write a program to reverse the linked list.

    class ListNode:
        def __init__(self, val=0, next=None):
            self.val = val
            self.next = next
    
    def reverse_linked_list(head):
        prev, curr = None, head
        while curr:
            next_node = curr.next
            curr.next = prev
            prev = curr
            curr = next_node
        return prev
    

     

  4. Write a program to detect a loop in a linked list using Floyd’s Cycle Detection Algorithm.

    def detect_loop(head):
        slow, fast = head, head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False

     

  5. Implement a program to check if two binary trees are identical.

    class TreeNode:
        def __init__(self, val=0, left=None, right=None):
            self.val = val
            self.left = left
            self.right = right
    
    def are_identical(root1, root2):
        if not root1 and not root2:
            return True
        if root1 and root2 and root1.val == root2.val:
            return are_identical(root1.left, root2.left) and are_identical(root1.right, root2.right)
        return False

     

Add a comment: