1-800-459-6847

Tcs Coding Questions 2021 -

Problem Statement: Given a number N, write a program to obtain a number M by reversing the digits of N. If M contains trailing zeros, they must be removed (which happens naturally during integer reversal).

Input:

Output:

Example:

Input: 54300 Output: 345

Solution (Java):

import java.util.Scanner;
public class Main 
    public static void main(String[] args) 
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int rev = 0;
while(N > 0) 
            int digit = N % 10;
            rev = rev * 10 + digit;
            N = N / 10;
System.out.println(rev);

Example: 45 → 45² = 2025 → split 20 + 25 = 45 → Yes.
Concept: Square, convert to string, split, sum, compare.

Problem:
Given N, print first N Fibonacci numbers.

Solution (Python - Recursive):

def fib(n):
    if n <= 1:
        return n
    return fib(n-1) + fib(n-2)

n = int(input()) for i in range(n): print(fib(i), end=" ")

Note: Recursion is inefficient for large N. TCS 2021 often asked for logic, not efficiency.


The questions in 2021 largely focused on:


| Question Type | Marks | |---------------|-------| | Easy (array reversal, pattern) | 15–25 marks | | Medium (Kaprekar, anagrams) | 25–40 marks | | Partial (some test cases pass) | Proportional marks |

Important: TCS does not award marks for compilation errors or runtime exceptions.

In the TCS NQT (Ninja and Digital roles), the coding section has specific constraints you must know before writing a single line of code. Tcs Coding Questions 2021

  • Input/Output: You must read input from stdin (Standard Input) and print output to stdout (Standard Output). Do not write input prompts (e.g., don't print "Enter a number:").
  • Languages Allowed: C, C++, Java, Python, Perl, C#.
  • Asked in: TCS Ninja (September 2021)

    Problem Statement:
    Write a program that takes an array of N integers. Calculate the sum of numbers present at even indices (0-based) and subtract the sum of numbers at odd indices. Return the absolute difference.

    Example:

    Approach (Python one-liner):

    arr = list(map(int, input().split()))
    even_sum = sum(arr[i] for i in range(0, len(arr), 2))
    odd_sum = sum(arr[i] for i in range(1, len(arr), 2))
    print(abs(even_sum - odd_sum))