TCS NQT 2025: Top 25 Coding Questions and Tips
Your ultimate guide to acing TCS NQT 2025 with 25 key coding questions in C++ and prep tips from Jobs Addaa!
The TCS National Qualifier Test (NQT) 2025 is your gateway to a career at Tata Consultancy Services. This **TCS NQT 2025 post** from Jobs Addaa delivers 25 essential coding questions solved in C++, blending arrays, numbers, strings, and more, along with must-know tips to excel. Scroll through and kickstart your prep!
Why This TCS NQT 2025 Post?
The coding round tests diverse skills—efficiency, logic, and variety. This post covers key TCS NQT patterns with concise C++ solutions and strategies, perfect for freshers aiming for success in 2025.
Top 25 Coding Questions for TCS NQT 2025
1. Check if a Number is Palindrome
Approach: Reverse the number by extracting digits and compare with the original. Time Complexity: O(log n).
#includeusing namespace std; bool isPalindrome(int num) { int rev = 0, temp = num; while (temp > 0) { rev = rev * 10 + temp % 10; temp /= 10; } return rev == num; } int main() { int num = 121; cout << (isPalindrome(num) ? "Yes" : "No"); // Output: Yes return 0; }
2. Find the Smallest Number in an Array
Approach: Linearly scan, updating the minimum value. Time Complexity: O(n).
#includeusing namespace std; int findSmallest(int arr[], int n) { int min = arr[0]; for (int i = 1; i < n; i++) if (arr[i] < min) min = arr[i]; return min; } int main() { int arr[] = {4, 2, 9, 1, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << findSmallest(arr, n); // Output: 1 return 0; }
3. Check if a String is Palindrome
Approach: Compare characters from both ends using two pointers. Time Complexity: O(n).
#includeusing namespace std; bool isPalindrome(string s) { int left = 0, right = s.length() - 1; while (left < right) { if (s[left++] != s[right--]) return false; } return true; } int main() { string s = "radar"; cout << (isPalindrome(s) ? "Yes" : "No"); // Output: Yes return 0; }
4. Reverse a Given Array
Approach: Swap elements from start to end. Time Complexity: O(n/2) = O(n).
#includeusing namespace std; void reverseArray(int arr[], int n) { for (int i = 0; i < n / 2; i++) swap(arr[i], arr[n - 1 - i]); } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); reverseArray(arr, n); for (int i = 0; i < n; i++) cout << arr[i] << " "; // Output: 5 4 3 2 1 return 0; }
5. Check if a Number is Prime
Approach: Test divisibility up to sqrt(n). Time Complexity: O(√n).
#includeusing namespace std; bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int main() { int num = 17; cout << (isPrime(num) ? "Yes" : "No"); // Output: Yes return 0; }
6. Bubble Sort Algorithm
Approach: Repeatedly swap adjacent elements if out of order. Time Complexity: O(n²).
#includeusing namespace std; void bubbleSort(int arr[], int n) { for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]); } int main() { int arr[] = {5, 2, 9, 1, 5}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); for (int i = 0; i < n; i++) cout << arr[i] << " "; // Output: 1 2 5 5 9 return 0; }
7. Factorial of a Number
Approach: Multiply numbers from 1 to n iteratively. Time Complexity: O(n).
#includeusing namespace std; long long factorial(int n) { long long fact = 1; for (int i = 1; i <= n; i++) fact *= i; return fact; } int main() { int n = 5; cout << factorial(n); // Output: 120 return 0; }
8. Count Frequency of Each Element in an Array
Approach: Use a map to count occurrences. Time Complexity: O(n).
#includeusing namespace std; void countFrequency(int arr[], int n) { unordered_map freq; for (int i = 0; i < n; i++) freq[arr[i]]++; for (auto x : freq) cout << x.first << ": " << x.second << " "; } int main() { int arr[] = {1, 2, 2, 3, 1}; int n = sizeof(arr) / sizeof(arr[0]); countFrequency(arr, n); // Output: 1: 2 2: 2 3: 1 return 0; }
9. Reverse a String
Approach: Swap characters from ends to middle. Time Complexity: O(n/2) = O(n).
#includeusing namespace std; void reverseString(string &s) { int left = 0, right = s.length() - 1; while (left < right) swap(s[left++], s[right--]); } int main() { string s = "hello"; reverseString(s); cout << s; // Output: olleh return 0; }
10. Sum of First N Natural Numbers
Approach: Use the formula n*(n+1)/2 for efficiency. Time Complexity: O(1).
#includeusing namespace std; int sumNatural(int n) { return n * (n + 1) / 2; } int main() { int n = 5; cout << sumNatural(n); // Output: 15 return 0; }
11. Remove Duplicates from a Sorted Array
Approach: Overwrite duplicates in-place with a pointer. Time Complexity: O(n).
#includeusing namespace std; int removeDuplicates(int arr[], int n) { if (n == 0 || n == 1) return n; int j = 1; for (int i = 1; i < n; i++) if (arr[i] != arr[j - 1]) arr[j++] = arr[i]; return j; } int main() { int arr[] = {1, 1, 2, 2, 3}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n); int newSize = removeDuplicates(arr, n); for (int i = 0; i < newSize; i++) cout << arr[i] << " "; // Output: 1 2 3 return 0; }
12. GCD of Two Numbers
Approach: Use Euclidean algorithm recursively. Time Complexity: O(log(min(a,b))).
#includeusing namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a = 48, b = 18; cout << gcd(a, b); // Output: 6 return 0; }
13. Check if a Number is Armstrong
Approach: Sum each digit raised to the power of number length, compare with original. Time Complexity: O(log n).
#includeusing namespace std; bool isArmstrong(int num) { int temp = num, sum = 0, digits = log10(num) + 1; while (temp > 0) { int digit = temp % 10; sum += pow(digit, digits); temp /= 10; } return sum == num; } int main() { int num = 153; cout << (isArmstrong(num) ? "Yes" : "No"); // Output: Yes return 0; }
14. Find All Non-Repeating Elements in an Array
Approach: Use a map to count, print frequency = 1. Time Complexity: O(n).
#includeusing namespace std; void findNonRepeating(int arr[], int n) { unordered_map freq; for (int i = 0; i < n; i++) freq[arr[i]]++; for (auto x : freq) if (x.second == 1) cout << x.first << " "; } int main() { int arr[] = {1, 2, 2, 3, 1}; int n = sizeof(arr) / sizeof(arr[0]); findNonRepeating(arr, n); // Output: 3 return 0; }
15. Calculate Frequency of Characters in a String
Approach: Use a map to count each character’s occurrences. Time Complexity: O(n).
#includeusing namespace std; void charFrequency(string s) { unordered_map freq; for (char c : s) freq[c]++; for (auto x : freq) cout << x.first << ": " << x.second << " "; } int main() { string s = "hello"; charFrequency(s); // Output: h: 1 e: 1 l: 2 o: 1 return 0; }
16. Print Fibonacci up to Nth Term
Approach: Generate Fibonacci sequence iteratively up to n terms. Time Complexity: O(n).
#includeusing namespace std; void printFibonacci(int n) { int a = 0, b = 1; cout << a << " " << b << " "; for (int i = 2; i < n; i++) { int next = a + b; cout << next << " "; a = b; b = next; } } int main() { int n = 5; printFibonacci(n); // Output: 0 1 1 2 3 return 0; }
17. Selection Sort Algorithm
Approach: Find the minimum element in each pass and place it at the start. Time Complexity: O(n²).
#includeusing namespace std; void selectionSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; swap(arr[i], arr[min_idx]); } } int main() { int arr[] = {5, 2, 9, 1, 5}; int n = sizeof(arr) / sizeof(arr[0]); selectionSort(arr, n); for (int i = 0; i < n; i++) cout << arr[i] << " "; // Output: 1 2 5 5 9 return 0; }
18. Check if a Number is Perfect
Approach: Sum proper divisors and compare with the number. Time Complexity: O(√n).
#includeusing namespace std; bool isPerfect(int n) { int sum = 1; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { sum += i; if (i * i != n) sum += n / i; } } return sum == n && n != 1; } int main() { int n = 6; cout << (isPerfect(n) ? "Yes" : "No"); // Output: Yes return 0; }
19. Rotate Array by K Elements (Block Swap)
Approach: Use block swap to rotate in-place. Time Complexity: O(n).
#includeusing namespace std; void swap(int arr[], int a, int b, int d) { for (int i = 0; i < d; i++) swap(arr[a + i], arr[b + i]); } void blockSwap(int arr[], int n, int k) { k = k % n; if (k == 0) return; int a = k, b = n - k; while (a != b) { if (a < b) { swap(arr, a, n - b, a); b -= a; } else { swap(arr, a - b, n - b, b); a -= b; } } swap(arr, 0, n - a, a); } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); blockSwap(arr, n, 2); for (int i = 0; i < n; i++) cout << arr[i] << " "; // Output: 3 4 5 1 2 return 0; }
20. Reverse Digits of a Number
Approach: Extract digits and build the reversed number. Time Complexity: O(log n).
#includeusing namespace std; int reverseDigits(int num) { int rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num /= 10; } return rev; } int main() { int num = 1234; cout << reverseDigits(num); // Output: 4321 return 0; }
21. Check if Two Strings are Anagram
Approach: Sort both strings and compare. Time Complexity: O(n log n).
#includeusing namespace std; bool areAnagram(string s1, string s2) { if (s1.length() != s2.length()) return false; sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); return s1 == s2; } int main() { string s1 = "listen", s2 = "silent"; cout << (areAnagram(s1, s2) ? "Yes" : "No"); // Output: Yes return 0; }
22. Power of a Number
Approach: Multiply base by itself exponent times. Time Complexity: O(n).
#includeusing namespace std; long long power(int base, int exp) { long long result = 1; for (int i = 0; i < exp; i++) result *= base; return result; } int main() { int base = 2, exp = 3; cout << power(base, exp); // Output: 8 return 0; }
23. Decimal to Binary Conversion
Approach: Repeatedly divide by 2, collect remainders in reverse. Time Complexity: O(log n).
#includeusing namespace std; string decimalToBinary(int n) { string binary = ""; while (n > 0) { binary = (n % 2 == 0 ? "0" : "1") + binary; n /= 2; } return binary.empty() ? "0" : binary; } int main() { int n = 10; cout << decimalToBinary(n); // Output: 1010 return 0; }
24. Maximum Product Subarray in an Array
Approach: Track max/min products for negatives, update global max. Time Complexity: O(n).
#includeusing namespace std; int maxProduct(int arr[], int n) { int maxSoFar = arr[0], minSoFar = arr[0], result = maxSoFar; for (int i = 1; i < n; i++) { int curr = arr[i]; int tempMax = max({curr, maxSoFar * curr, minSoFar * curr}); minSoFar = min({curr, maxSoFar * curr, minSoFar * curr}); maxSoFar = tempMax; result = max(result, maxSoFar); } return result; } int main() { int arr[] = {6, -3, -10, 0, 2}; int n = sizeof(arr) / sizeof(arr[0]); cout << maxProduct(arr, n); // Output: 180 return 0; }
25. Count Number of Words in a String
Approach: Count spaces and add 1, handling edge cases. Time Complexity: O(n).
#includeusing namespace std; int countWords(string s) { int count = 0; for (int i = 0; i < s.length(); i++) if (s[i] == ' ' && i > 0 && s[i-1] != ' ') count++; return s.empty() || s[s.length()-1] == ' ' ? count : count + 1; } int main() { string s = "Hello World"; cout << countWords(s); // Output: 2 return 0; }
Essential TCS NQT 2025 Prep Tips
- Know the Basics: Arrays, strings, and numbers are TCS NQT staples—master them.
- Optimize: Aim for O(n) or better—efficiency wins points.
- Practice: Use Jobs Addaa’s coding tools to test these daily.
- Time It: Solve in 15-20 minutes to match exam pace.
- Debug: Test edge cases (negatives, zeros) every time.
Why Jobs Addaa’s TCS NQT 2025 Sheet?
This sheet delivers 25 critical coding questions with C++ solutions and pro tips, tailored for TCS NQT 2025 success. Pair it with Jobs Addaa’s prep resources to ace your coding round—start now!
Prepare with Jobs AddaaConclusion
With this TCS NQT 2025 sheet from Jobs Addaa, you’ve got 25 key coding questions and tips to shine in the coding round. Practice these, and your TCS dream is within reach—start today!
Discover more from Jobs Addaa
Subscribe to get the latest posts sent to your email.