TCS Placement Papers 2024
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 2025
Complete TCS Guide
Access complete TCS placement papers guide with eligibility, process, and preparation strategy.
TCS NQT Coding Section Overview
Section titled “TCS NQT Coding Section Overview”TCS NQT Coding Section Breakdown:
| Section | Questions | Time | Difficulty | Focus Areas |
|---|---|---|---|---|
| Programming Logic | 10-15 | 20 min | Easy-Medium | Output prediction, loops, arrays, pointers |
| Hands-on Coding | 1-2 | 50 min | Medium | Arrays, strings, basic algorithms, recursion |
Total Coding Questions: 11-17 questions in 70 minutes
Languages Allowed: C, C++, Java, Python
Programming Logic Questions (MCQs)
Section titled “Programming Logic Questions (MCQs)”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++xis 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 = xy.append(4)print(len(x))Analysis:
y = xcreates 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
Q6: How many times will the loop execute?
int i = 0;while (i < 10) { if (i % 2 == 0) { printf("%d ", i); } i++;}Analysis:
- Loop runs for i = 0 to 9 (10 iterations total)
- printf executes only when i is even (0, 2, 4, 6, 8)
- Output: 0 2 4 6 8
Answer: Loop executes 10 times, printf executes 5 times
Q7: What is the sum after the loop?
int sum = 0;for (int i = 1; i <= 5; i++) { sum += i * i;}printf("%d", sum);Analysis:
- i=1: sum = 0 + 1 = 1
- i=2: sum = 1 + 4 = 5
- i=3: sum = 5 + 9 = 14
- i=4: sum = 14 + 16 = 30
- i=5: sum = 30 + 25 = 55
Answer: 55
Q8: What will be the output?
int i = 1, j = 1;while (i <= 3) { while (j <= 3) { printf("%d%d ", i, j); j++; } i++;}Analysis:
- Inner loop runs completely once (j: 1→2→3→4)
- j is not reset, so inner loop never runs again
- Output: 11 12 13
Answer: 11 12 13
Q9: What will be the output?
int arr[] = {10, 20, 30, 40, 50};int *ptr = arr;printf("%d", *(ptr + 3));Analysis:
- ptr points to arr[0] = 10
- ptr + 3 points to arr[3] = 40
- *(ptr + 3) dereferences to get value
Answer: 40
Q10: What will be the output?
int arr[] = {1, 2, 3, 4, 5};printf("%d", sizeof(arr)/sizeof(arr[0]));Analysis:
- sizeof(arr) = 20 bytes (5 integers × 4 bytes each)
- sizeof(arr[0]) = 4 bytes (size of one integer)
- 20/4 = 5 (number of elements)
Answer: 5
Hands-on Coding Problems
Section titled “Hands-on Coding Problems”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: 34Solution (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 -1Time 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: 17Output: true (17 is prime)
Input: 18Output: 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 TrueTime 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: 153Output: true (1³ + 5³ + 3³ = 1 + 125 + 27 = 153)
Input: 123Output: falseSolution (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 == originalTime 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 = 8Output: 0 1 1 2 3 5 8 13Solution (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 resultTime 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: 121Output: true
Input: 123Output: falseSolution (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: 5Output: 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 resultTime Complexity: O(n) | Space Complexity: O(1)
Sum of Digits
Problem: Find the sum of all digits in a number.
Example:
Input: 12345Output: 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)
Array Rotation
Problem: Rotate an array to the right by k positions.
Example:
Input: nums = [1,2,3,4,5,6,7], k = 3Output: [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--; }}Solution (Python):
def rotate(nums, k): n = len(nums) k = k % n nums[:] = nums[n-k:] + nums[:n-k]Time Complexity: O(n) | Space Complexity: O(1)
Find Missing Number
Problem: Find the missing number in an array containing 1 to n.
Example:
Input: [1, 2, 4, 5, 6] (n = 6)Output: 3Solution (Java):
public int findMissing(int[] nums, int n) { int expectedSum = n * (n + 1) / 2; int actualSum = 0;
for (int num : nums) { actualSum += num; }
return expectedSum - actualSum;}Solution (Python):
def find_missing(nums, n): expected_sum = n * (n + 1) // 2 actual_sum = sum(nums) return expected_sum - actual_sumTime Complexity: O(n) | Space Complexity: O(1)
Remove Duplicates from Sorted Array
Problem: Remove duplicates in-place from a sorted array.
Example:
Input: [1, 1, 2, 2, 3, 4, 4]Output: 4 (array becomes [1, 2, 3, 4, ...])Solution (Java):
public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0;
int j = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[j]) { j++; nums[j] = nums[i]; } }
return j + 1;}Solution (Python):
def remove_duplicates(nums): if not nums: return 0
j = 0 for i in range(1, len(nums)): if nums[i] != nums[j]: j += 1 nums[j] = nums[i]
return j + 1Time Complexity: O(n) | Space Complexity: O(1)
Count Vowels and Consonants
Problem: Count the number of vowels and consonants in a string.
Example:
Input: "Hello World"Output: Vowels: 3, Consonants: 7Solution (Java):
public int[] countVowelsConsonants(String s) { int vowels = 0, consonants = 0; String vowelStr = "aeiouAEIOU";
for (char c : s.toCharArray()) { if (Character.isLetter(c)) { if (vowelStr.indexOf(c) != -1) { vowels++; } else { consonants++; } } }
return new int[]{vowels, consonants};}Solution (Python):
def count_vowels_consonants(s): vowels = set('aeiouAEIOU') vowel_count = 0 consonant_count = 0
for char in s: if char.isalpha(): if char in vowels: vowel_count += 1 else: consonant_count += 1
return vowel_count, consonant_countTime Complexity: O(n) | Space Complexity: O(1)
Two Sum
Problem: Find two numbers in an array that add up to a target sum.
Example:
Input: nums = [2, 7, 11, 15], target = 9Output: [0, 1] (nums[0] + nums[1] = 2 + 7 = 9)Solution (Java):
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) { return new int[]{map.get(complement), i}; } map.put(nums[i], i); }
return new int[]{};}Solution (Python):
def two_sum(nums, target): seen = {}
for i, num in enumerate(nums): complement = target - num if complement in seen: return [seen[complement], i] seen[num] = i
return []Time Complexity: O(n) | Space Complexity: O(n)
GCD of Two Numbers
Problem: Find the Greatest Common Divisor of two numbers.
Example:
Input: a = 48, b = 18Output: 6Solution (Java):
public int gcd(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a;}Solution (Python):
def gcd(a, b): while b: a, b = b, a % b return aTime Complexity: O(log(min(a,b))) | Space Complexity: O(1)
Check Anagram
Problem: Check if two strings are anagrams of each other.
Example:
Input: s1 = "listen", s2 = "silent"Output: trueSolution (Java):
public boolean isAnagram(String s1, String s2) { if (s1.length() != s2.length()) return false;
int[] count = new int[26];
for (int i = 0; i < s1.length(); i++) { count[s1.charAt(i) - 'a']++; count[s2.charAt(i) - 'a']--; }
for (int c : count) { if (c != 0) return false; }
return true;}Solution (Python):
def is_anagram(s1, s2): return sorted(s1.lower()) == sorted(s2.lower())Time Complexity: O(n) or O(n log n) for sorting | Space Complexity: O(1) or O(n)
Merge Two Sorted Arrays
Problem: Merge two sorted arrays into one sorted array.
Example:
Input: arr1 = [1, 3, 5], arr2 = [2, 4, 6]Output: [1, 2, 3, 4, 5, 6]Solution (Java):
public int[] mergeSorted(int[] arr1, int[] arr2) { int[] result = new int[arr1.length + arr2.length]; int i = 0, j = 0, k = 0;
while (i < arr1.length && j < arr2.length) { if (arr1[i] <= arr2[j]) { result[k++] = arr1[i++]; } else { result[k++] = arr2[j++]; } }
while (i < arr1.length) { result[k++] = arr1[i++]; }
while (j < arr2.length) { result[k++] = arr2[j++]; }
return result;}Solution (Python):
def merge_sorted(arr1, arr2): result = [] i = j = 0
while i < len(arr1) and j < len(arr2): if arr1[i] <= arr2[j]: result.append(arr1[i]) i += 1 else: result.append(arr2[j]) j += 1
result.extend(arr1[i:]) result.extend(arr2[j:]) return resultTime Complexity: O(n + m) | Space Complexity: O(n + m)
Pattern Printing - Right Triangle
Problem: Print a right triangle pattern of stars.
Example:
Input: n = 5Output:***************Solution (Java):
public void printRightTriangle(int n) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }}Solution (Python):
def print_right_triangle(n): for i in range(1, n + 1): print('*' * i)Time Complexity: O(n²) | Space Complexity: O(1)
Pattern Printing - Pyramid
Problem: Print a pyramid pattern of stars.
Example:
Input: n = 5Output: * *** ***** ****************Solution (Java):
public void printPyramid(int n) { for (int i = 1; i <= n; i++) { // Print spaces for (int j = 1; j <= n - i; j++) { System.out.print(" "); } // Print stars for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); }}Solution (Python):
def print_pyramid(n): for i in range(1, n + 1): spaces = ' ' * (n - i) stars = '*' * (2 * i - 1) print(spaces + stars)Time Complexity: O(n²) | Space Complexity: O(1)
Palindrome String Check
Problem: Check if a string is a palindrome (ignoring case and non-alphanumeric characters).
Example:
Input: "A man, a plan, a canal: Panama"Output: trueSolution (Java):
public boolean isPalindrome(String s) { int left = 0, right = s.length() - 1;
while (left < right) { while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { left++; } while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { right--; }
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { return false; } left++; right--; }
return true;}Solution (Python):
def is_palindrome(s): cleaned = ''.join(c.lower() for c in s if c.isalnum()) return cleaned == cleaned[::-1]Time Complexity: O(n) | Space Complexity: O(1) or O(n)
First Non-Repeating Character
Problem: Find the first non-repeating character in a string.
Example:
Input: "leetcode"Output: 'l' (index 0)
Input: "loveleetcode"Output: 'v' (index 2)Solution (Java):
public char firstUniqChar(String s) { int[] count = new int[26];
for (char c : s.toCharArray()) { count[c - 'a']++; }
for (char c : s.toCharArray()) { if (count[c - 'a'] == 1) { return c; } }
return '\0';}Solution (Python):
def first_unique_char(s): from collections import Counter count = Counter(s)
for i, char in enumerate(s): if count[char] == 1: return char return NoneTime Complexity: O(n) | Space Complexity: O(1) (fixed 26 chars)
Longest Common Prefix
Problem: Find the longest common prefix among an array of strings.
Example:
Input: ["flower", "flow", "flight"]Output: "fl"Solution (Java):
public String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return ""; } }
return prefix;}Solution (Python):
def longest_common_prefix(strs): if not strs: return ""
prefix = strs[0]
for s in strs[1:]: while not s.startswith(prefix): prefix = prefix[:-1] if not prefix: return ""
return prefixTime Complexity: O(S) where S is sum of all characters | Space Complexity: O(1)
String Compression
Problem: Compress a string using counts of repeated characters.
Example:
Input: "aabcccccaaa"Output: "a2b1c5a3"Solution (Java):
public String compress(String s) { if (s == null || s.length() == 0) return s;
StringBuilder result = new StringBuilder(); int count = 1;
for (int i = 1; i <= s.length(); i++) { if (i < s.length() && s.charAt(i) == s.charAt(i - 1)) { count++; } else { result.append(s.charAt(i - 1)).append(count); count = 1; } }
return result.length() < s.length() ? result.toString() : s;}Solution (Python):
def compress(s): if not s: return s
result = [] count = 1
for i in range(1, len(s) + 1): if i < len(s) and s[i] == s[i - 1]: count += 1 else: result.append(s[i - 1] + str(count)) count = 1
compressed = ''.join(result) return compressed if len(compressed) < len(s) else sTime Complexity: O(n) | Space Complexity: O(n)
Practice Tips for TCS NQT Coding
Section titled “Practice Tips for TCS NQT Coding”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
TCS NQT Coding Section Strategy
Section titled “TCS NQT Coding Section Strategy”- Read the problem carefully - Understand input/output format
- Identify the pattern - Most TCS problems follow standard patterns
- Write clean code - Use meaningful variable names
- Test with examples - Verify with given test cases
- Handle edge cases - Empty arrays, negative numbers, large inputs
Related Resources
Section titled “Related Resources”TCS 2024 Papers
Previous year papers with coding questions and solutions
TCS 2025 Papers
Latest papers with current year coding questions
TCS Aptitude Questions
TCS aptitude questions with solutions
TCS Interview Experience
Real interview experiences from TCS candidates
TCS Preparation Guide
Comprehensive preparation strategy for TCS NQT
TCS Main Page
Complete TCS placement guide with eligibility, process, and salary details
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