Skip to content

TCS Coding Questions 2025 - Programming Logic & Hands-on Coding Problems with Solutions

Practice 25+ TCS placement paper coding questions with detailed solutions. Access TCS NQT programming logic questions and hands-on coding problems in C, C++, Java, Python for TCS placement 2025-2026.

TCS Placement Paper Coding Questions - Complete Guide

Section titled “TCS Placement Paper Coding Questions - Complete Guide”

Practice with 25+ TCS placement paper coding questions covering Programming Logic MCQs and Hands-on Coding problems. These questions are representative of what you’ll encounter in the TCS NQT (National Qualifier Test) and TCS Digital hiring exams.

What’s Included:

  • 10 Programming Logic Questions: Output prediction, loop analysis, pointer questions
  • 15+ Hands-on Coding Problems: Easy and Medium level problems with solutions
  • Multiple Language Solutions: Java, Python, and C++ solutions
  • Time Complexity Analysis: Every solution includes complexity analysis

TCS Placement Papers 2024

Access 2024 TCS NQT questions with solutions and exam pattern analysis.


View 2024 Papers →

TCS Placement Papers 2025

Practice latest 2025 TCS NQT questions with updated patterns.


View 2025 Papers →

Complete TCS Guide

Access complete TCS placement papers guide with eligibility, process, and preparation strategy.


View Complete Guide →

TCS NQT Coding Section Breakdown:

SectionQuestionsTimeDifficultyFocus Areas
Programming Logic10-1520 minEasy-MediumOutput prediction, loops, arrays, pointers
Hands-on Coding1-250 minMediumArrays, strings, basic algorithms, recursion

Total Coding Questions: 11-17 questions in 70 minutes

Languages Allowed: C, C++, Java, Python

Q1: What will be the output?
#include <stdio.h>
int main() {
int x = 5;
printf("%d", x++ + ++x);
return 0;
}

Analysis:

  • x++ is post-increment: uses current value (5), then increments x to 6
  • ++x is pre-increment: increments x (6→7), then uses value 7
  • Expression evaluates to: 5 + 7 = 12

Answer: 12

Q2: What will be the output?
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("%d", a > b ? a : b);
return 0;
}

Analysis:

  • Ternary operator: condition ? value_if_true : value_if_false
  • 10 > 5 is true, so it returns ‘a’ which is 10

Answer: 10

Q3: What will be the output?
#include <stdio.h>
int main() {
int i;
for(i = 0; i < 5; i++);
printf("%d", i);
return 0;
}

Analysis:

  • Note the semicolon after the for loop - it’s an empty loop body
  • Loop runs 5 times (i: 0→1→2→3→4→5), then exits when i=5
  • After loop, i = 5

Answer: 5

Q4: What will be the output?
public class Test {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2);
}
}

Analysis:

  • String literals are stored in String pool
  • Both s1 and s2 point to same object in pool
  • == compares references, which are same here

Answer: true

Q5: What will be the output?
x = [1, 2, 3]
y = x
y.append(4)
print(len(x))

Analysis:

  • y = x creates a reference, not a copy
  • Both x and y point to the same list
  • Appending to y also modifies x
  • Length of x is now 4

Answer: 4

Second Largest Element in Array

Problem: Find the second largest element in an array without sorting.

Example:

Input: [12, 35, 1, 10, 34, 1]
Output: 34

Solution (Java):

public int findSecondLargest(int[] arr) {
if (arr.length < 2) return -1;
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
return secondLargest == Integer.MIN_VALUE ? -1 : secondLargest;
}

Solution (Python):

def find_second_largest(arr):
if len(arr) < 2:
return -1
largest = second_largest = float('-inf')
for num in arr:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
return second_largest if second_largest != float('-inf') else -1

Time Complexity: O(n) | Space Complexity: O(1)

Reverse Words in a String

Problem: Given a string, reverse the order of words.

Example:

Input: "the sky is blue"
Output: "blue is sky the"

Solution (Java):

public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
StringBuilder result = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]);
if (i > 0) result.append(" ");
}
return result.toString();
}

Solution (Python):

def reverse_words(s):
words = s.split()
return ' '.join(words[::-1])

Time Complexity: O(n) | Space Complexity: O(n)

Prime Number Check

Problem: Determine if a given number is prime.

Example:

Input: 17
Output: true (17 is prime)
Input: 18
Output: false (18 is not prime)

Solution (Java):

public boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}

Solution (Python):

def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True

Time Complexity: O(√n) | Space Complexity: O(1)

Armstrong Number

Problem: Check if a number is an Armstrong number (sum of cubes of digits equals the number).

Example:

Input: 153
Output: true (1³ + 5³ + 3³ = 1 + 125 + 27 = 153)
Input: 123
Output: false

Solution (Java):

public boolean isArmstrong(int n) {
int original = n;
int digits = String.valueOf(n).length();
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += Math.pow(digit, digits);
n /= 10;
}
return sum == original;
}

Solution (Python):

def is_armstrong(n):
digits = len(str(n))
original = n
sum_val = 0
while n > 0:
digit = n % 10
sum_val += digit ** digits
n //= 10
return sum_val == original

Time Complexity: O(d) where d is number of digits | Space Complexity: O(1)

Fibonacci Series

Problem: Generate Fibonacci series up to n terms.

Example:

Input: n = 8
Output: 0 1 1 2 3 5 8 13

Solution (Java):

public void printFibonacci(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}

Solution (Python):

def fibonacci(n):
a, b = 0, 1
result = []
for _ in range(n):
result.append(a)
a, b = b, a + b
return result

Time Complexity: O(n) | Space Complexity: O(1) or O(n) if storing

Palindrome Check (Number)

Problem: Check if a number reads the same forwards and backwards.

Example:

Input: 121
Output: true
Input: 123
Output: false

Solution (Java):

public boolean isPalindrome(int x) {
if (x < 0) return false;
int original = x;
int reversed = 0;
while (x > 0) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
return original == reversed;
}

Solution (Python):

def is_palindrome(x):
if x < 0:
return False
return str(x) == str(x)[::-1]

Time Complexity: O(log n) | Space Complexity: O(1)

Factorial Calculation

Problem: Calculate the factorial of a number.

Example:

Input: 5
Output: 120 (5! = 5×4×3×2×1 = 120)

Solution (Java):

public long factorial(int n) {
if (n <= 1) return 1;
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}

Solution (Python):

def factorial(n):
if n <= 1:
return 1
result = 1
for i in range(2, n + 1):
result *= i
return result

Time Complexity: O(n) | Space Complexity: O(1)

Sum of Digits

Problem: Find the sum of all digits in a number.

Example:

Input: 12345
Output: 15 (1+2+3+4+5 = 15)

Solution (Java):

public int sumOfDigits(int n) {
int sum = 0;
n = Math.abs(n);
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}

Solution (Python):

def sum_of_digits(n):
return sum(int(d) for d in str(abs(n)))

Time Complexity: O(d) where d is number of digits | Space Complexity: O(1)

Master One Language

  • Choose C, C++, Java, or Python
  • Master syntax, data types, and control structures
  • Know standard library functions
  • Practice writing code without IDE autocomplete

Focus on Fundamentals

  • Arrays, strings, loops, conditionals
  • Basic algorithms: sorting, searching
  • Pattern printing problems
  • Mathematical operations (GCD, factorial, prime)

Time Management

  • Programming Logic: 1-2 min per MCQ
  • Hands-on Coding: 25-30 min per problem
  • Always test with sample inputs
  • Handle edge cases (empty input, single element)

Common Mistakes to Avoid

  • Off-by-one errors in loops
  • Integer overflow for large numbers
  • Not handling null/empty inputs
  • Forgetting to return correct data type
  1. Read the problem carefully - Understand input/output format
  2. Identify the pattern - Most TCS problems follow standard patterns
  3. Write clean code - Use meaningful variable names
  4. Test with examples - Verify with given test cases
  5. Handle edge cases - Empty arrays, negative numbers, large inputs
Practice More TCS Coding Questions →

TCS Main Page

Complete TCS placement guide with eligibility, process, and salary details

View Main Page →


Practice TCS coding questions regularly to master programming fundamentals! Focus on array manipulation, string processing, and mathematical problems. These are the most common problem types in TCS NQT.

Pro Tip: TCS values clean, working code over complex solutions. Start with a brute force approach, then optimize if time permits.

Last updated: February 2026