Skip to content

TCS Coding Questions - Programming Logic & Hands-on Coding Problems

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

This page contains TCS coding questions from TCS NQT placement papers with detailed solutions. Practice TCS programming logic questions and TCS hands-on coding problems to prepare for TCS NQT coding section.

TCS NQT Coding Section Breakdown:

  • Programming Logic: 10-15 questions, 20 minutes (MCQs)
  • Hands-on Coding: 1-2 problems, 50 minutes (C/C++/Java/Python)

Total Coding Questions: 11-17 questions

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

Solution:

x++ is post-increment: uses value 5, then increments to 6 ++x is pre-increment: increments 6 to 7, then uses value 7

Result: 5 + 7 = 12

Answer: 12

Q2: How many times will the loop execute?
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}

Solution:

Loop executes for i = 0, 1, 2, 3, 4 (5 times)

Answer: 5 times

Q3: Find the second largest element in an array.

Problem Statement: Given an array of integers, find the second largest element.

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;
}

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

Q4: Reverse words in a string.

Problem Statement: 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();
}

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

Q5: Rotate an array to the right by k positions.

Problem Statement: Given an array, rotate it to the right by k steps.

Example:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]

Solution (Java):

public void rotate(int[] nums, int k) {
int n = nums.length;
k = k % n;
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}

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

Q6: Check if a number is palindrome.

Problem Statement: Given a number, determine if it is a palindrome.

Example:

Input: 121
Output: true

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;
}

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

Q7: Calculate factorial of a number.

Problem Statement: Given a number n, calculate n! (factorial).

Example:

Input: 5
Output: 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;
}

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

Master One Language

  • Choose C, C++, Java, or Python
  • Master syntax, data structures, and basic algorithms
  • Practice daily coding problems

Focus on Fundamentals

  • Arrays, strings, loops, conditionals
  • Basic algorithms: sorting, searching
  • Don’t focus on advanced topics

Time Management

  • Programming Logic: 1-2 min per question
  • Hands-on Coding: 25-30 min per problem
  • Test your code with sample inputs

Practice TCS coding questions regularly to master programming fundamentals!