TCS Placement Papers 2024
Access 2024 TCS NQT questions with solutions and exam pattern analysis.
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:
TCS Placement Papers 2024
Access 2024 TCS NQT questions with solutions and exam pattern analysis.
TCS Placement Papers 2025
Practice latest 2025 TCS NQT questions with updated patterns.
Complete TCS guide
Access complete TCS placement papers guide with eligibility, process, and preparation strategy.
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
#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 7Answer: 12
#include <stdio.h>int main() { int a = 10, b = 5; printf("%d", a > b ? a : b); return 0;}Analysis:
Answer: 10
#include <stdio.h>int main() { int i; for(i = 0; i < 5; i++); printf("%d", i); return 0;}Analysis:
Answer: 5
public class Test { public static void main(String[] args) { String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1 == s2); }}Analysis:
== compares references, which are same hereAnswer: true
x = [1, 2, 3]y = xy.append(4)print(len(x))Analysis:
y = x creates a reference, not a copyAnswer: 4
int i = 0;while (i < 10) { if (i % 2 == 0) { printf("%d ", i); } i++;}Analysis:
Answer: Loop executes 10 times, printf executes 5 times
int sum = 0;for (int i = 1; i <= 5; i++) { sum += i * i;}printf("%d", sum);Analysis:
Answer: 55
int i = 1, j = 1;while (i <= 3) { while (j <= 3) { printf("%d%d ", i, j); j++; } i++;}Analysis:
Answer: 11 12 13
int arr[] = {10, 20, 30, 40, 50};int *ptr = arr;printf("%d", *(ptr + 3));Analysis:
Answer: 40
int arr[] = {1, 2, 3, 4, 5};printf("%d", sizeof(arr)/sizeof(arr[0]));Analysis:
Answer: 5
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)
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)
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)
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)
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Master one language
Focus on fundamentals
Time management
Common mistakes to avoid