TCS Coding Round Questions with Solutions
Last updated 2026-11-19
The TCS coding round rewards clean handling of simple-looking problems. This page works through eleven problems in the style that round is commonly reported to use, from Armstrong numbers to a CodeVita-flavoured digit-product problem, each with Python and Java and the mistakes that cost marks.
Pair it with TCS interview questions for the spoken rounds and how to crack the DSA coding round for general strategy.
| Coding component | Advanced IT programming in the IT NQT variant (TCS iON) |
|---|---|
| Languages | Python and Java shown here |
| Difficulty | Easy to hard, one hard CodeVita-style item |
What the TCS coding round looks like
TCS's iON page lists 'advanced IT programming (coding)' as a component of the IT NQT variant, alongside cognitive and aptitude sections. The problems below follow the style that round is commonly reported to use: short statements on arrays, strings and number theory, one or two problems per sitting, and an expectation that you handle edge cases yourself. The hardest items resemble what the separate CodeVita contest asks, so I have marked those as hard. These are practice problems in the style of the topics, not a previous paper.
How to practise
- Solve each problem first with brute force, then improve it; the first form is often enough for a test case that passes.
- Read input format carefully. Many failures are from reading a line as one integer when it holds several.
- Test with empty input, one element, all equal elements, negatives and the maximum limit.
- Know one language well. Both Python and Java are fine; C and C++ are also accepted on iON in many reports, so check the instructions on the day.
TCS Coding Round Questions with Solutions: questions with worked answers
1 Armstrong numbers in a range
easyAn n-digit number is Armstrong if the sum of each digit raised to n equals the number (153 = 1+125+27). For each number in [lo, hi], count digits, sum powers, compare. Complexity: O((hi-lo) * d) time, O(1) space. Common mistakes: computing the digit count once with a float log (precision error) instead of len(str(n)), and raising to a fixed power 3 for every number.
pythondef armstrong(lo, hi): out = [] for n in range(lo, hi + 1): d = len(str(n)) if sum(int(c) ** d for c in str(n)) == n: out.append(n) return out print(armstrong(100, 999)) # [153, 370, 371, 407]javaimport java.util.*; class Solution { static List<Integer> armstrong(int lo, int hi) { List<Integer> out = new ArrayList<>(); for (int n = lo; n <= hi; n++) { int d = String.valueOf(n).length(), t = n, s = 0; while (t > 0) { s += (int) Math.pow(t % 10, d); t /= 10; } if (s == n) out.add(n); } return out; } }2 Second largest distinct element
easyTrack the largest and second largest in one pass, updating second only when the value is strictly between them. Return None/-1 when fewer than two distinct values exist. Complexity: O(n) time, O(1) space. Common mistakes: using sorting and picking index -2 (fails with duplicates like [5, 5, 3]) and initialising with 0, which breaks for negative arrays.
pythondef second_largest(a): first = second = None for x in a: if first is None or x > first: first, second = x, first elif x != first and (second is None or x > second): second = x return second print(second_largest([5, 5, 3, 9, 9])) # 5javaclass Solution { static long secondLargest(int[] a) { long first = Long.MIN_VALUE, second = Long.MIN_VALUE; for (int x : a) { if (x > first) { second = first; first = x; } else if (x < first && x > second) second = x; } return second == Long.MIN_VALUE ? -1 : second; } }3 Rotate an array right by k
mediumReduce k modulo n, then reverse the whole array, the first k elements and the remaining n-k elements. This rotates in place. Complexity: O(n) time, O(1) space. Common mistakes: not taking k % n (index errors when k > n) and reversing the wrong segment order.
pythondef rotate(a, k): n = len(a) if n == 0: return a k %= n def rev(i, j): while i < j: a[i], a[j] = a[j], a[i]; i += 1; j -= 1 rev(0, n - 1); rev(0, k - 1); rev(k, n - 1) return a print(rotate([1, 2, 3, 4, 5, 6, 7], 3)) # [5, 6, 7, 1, 2, 3, 4]javaclass Solution { static void rotate(int[] a, int k) { int n = a.length; if (n == 0) return; k %= n; rev(a, 0, n - 1); rev(a, 0, k - 1); rev(a, k, n - 1); } static void rev(int[] a, int i, int j) { while (i < j) { int t = a[i]; a[i++] = a[j]; a[j--] = t; } } }4 Balanced brackets
mediumPush opening brackets on a stack; on a closing bracket the top must be its partner. The string is balanced only if the stack is empty at the end. Complexity: O(n) time, O(n) space. Common mistakes: popping from an empty stack and forgetting the final emptiness check (so '((' passes).
pythondef balanced(s): pair = {')': '(', ']': '[', '}': '{'} st = [] for c in s: if c in '([{': st.append(c) elif c in pair: if not st or st.pop() != pair[c]: return False return not st print(balanced("{[()]}"), balanced("(]")) # True Falsejavaimport java.util.*; class Solution { static boolean balanced(String s) { Deque<Character> st = new ArrayDeque<>(); for (char c : s.toCharArray()) { if ("([{".indexOf(c) >= 0) st.push(c); else if (")]}".indexOf(c) >= 0) { if (st.isEmpty()) return false; char o = st.pop(); if ("([{".indexOf(o) != ")]}".indexOf(c)) return false; } } return st.isEmpty(); } }5 Largest sum of a contiguous subarray
mediumKadane's algorithm: at each element the best sum ending here is max(x, previous + x); the answer is the maximum seen. Complexity: O(n) time, O(1) space. Common mistakes: starting best at 0 (wrong when every number is negative) and resetting to 0 instead of to x.
pythondef max_subarray(a): cur = best = a[0] for x in a[1:]: cur = max(x, cur + x) best = max(best, cur) return best print(max_subarray([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # 6javaclass Solution { static int maxSubarray(int[] a) { int cur = a[0], best = a[0]; for (int i = 1; i < a.length; i++) { cur = Math.max(a[i], cur + a[i]); best = Math.max(best, cur); } return best; } }6 GCD and LCM of two numbers
easyEuclid's algorithm: gcd(a, b) = gcd(b, a mod b) until b is 0. Then lcm = a / gcd * b, dividing first to reduce overflow. Complexity: O(log min(a, b)) time, O(1) space. Common mistakes: computing a * b / gcd (can overflow int) and looping with subtraction, which is far too slow for large inputs.
pythondef gcd(a, b): while b: a, b = b, a % b return a def lcm(a, b): return a // gcd(a, b) * b print(gcd(48, 18), lcm(4, 6)) # 6 12javaclass Solution { static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a / gcd(a, b) * b; } }7 Spiral order of a matrix
mediumKeep four boundaries (top, bottom, left, right), walk right, down, left, up, and shrink a boundary after each side. Check the boundaries again before the left and up walks so a single row or column is not repeated. Complexity: O(m*n) time, O(1) extra space. Common mistakes: double-counting the middle row or column of non-square matrices.
pythondef spiral(m): out = [] if not m: return out t, b, l, r = 0, len(m) - 1, 0, len(m[0]) - 1 while t <= b and l <= r: out += m[t][l:r + 1]; t += 1 for i in range(t, b + 1): out.append(m[i][r]) r -= 1 if t <= b: out += m[b][l:r + 1][::-1]; b -= 1 if l <= r: for i in range(b, t - 1, -1): out.append(m[i][l]) l += 1 return out print(spiral([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) # [1,2,3,6,9,8,7,4,5]javaimport java.util.*; class Solution { static List<Integer> spiral(int[][] m) { List<Integer> out = new ArrayList<>(); if (m.length == 0) return out; int t = 0, b = m.length - 1, l = 0, r = m[0].length - 1; while (t <= b && l <= r) { for (int j = l; j <= r; j++) out.add(m[t][j]); t++; for (int i = t; i <= b; i++) out.add(m[i][r]); r--; if (t <= b) { for (int j = r; j >= l; j--) out.add(m[b][j]); b--; } if (l <= r) { for (int i = b; i >= t; i--) out.add(m[i][l]); l++; } } return out; } }8 Count ways to climb n stairs (1 or 2 steps)
mediumways(n) = ways(n-1) + ways(n-2), which is the Fibonacci recurrence. Keep only the last two values. Complexity: O(n) time, O(1) space. Common mistakes: plain recursion without memoisation (exponential time) and off-by-one base cases: ways(1)=1, ways(2)=2.
pythondef climb(n): a, b = 1, 1 for _ in range(n - 1): a, b = b, a + b return b print(climb(5)) # 8javaclass Solution { static long climb(int n) { long a = 1, b = 1; for (int i = 1; i < n; i++) { long t = a + b; a = b; b = t; } return b; } }9 Smallest number whose digit product equals N
hardThis is the CodeVita-style flavour: factor N by digits 9 down to 2, dividing greedily, collect the digits and print them in ascending order. If N is not 1 after the loop, there is no answer. Special-case N < 10 (answer N; and N = 0 gives 10 by convention). Complexity: O(log N) time, O(log N) space. Common mistakes: iterating digits upward (gives a longer, larger number) and forgetting the N = 1 case.
pythondef smallest(n): if n < 10: return n if n else 10 digits = [] for d in range(9, 1, -1): while n % d == 0: digits.append(d); n //= d return int(''.join(map(str, sorted(digits)))) if n == 1 else -1 print(smallest(100), smallest(13)) # 455 -1javaimport java.util.*; class Solution { static long smallest(int n) { if (n < 10) return n == 0 ? 10 : n; List<Integer> d = new ArrayList<>(); for (int x = 9; x > 1; x--) while (n % x == 0) { d.add(x); n /= x; } if (n != 1) return -1; Collections.sort(d); StringBuilder sb = new StringBuilder(); for (int x : d) sb.append(x); return Long.parseLong(sb.toString()); } }10 Longest palindromic substring
hardExpand around each centre (n odd centres and n-1 even ones), keep the longest span found. Simple and fast enough for NQT limits. Complexity: O(n^2) time, O(1) space. Common mistakes: only trying odd centres, so 'abba' fails, and returning length instead of the substring.
pythondef longest_pal(s): best = "" for c in range(len(s)): for l, r in ((c, c), (c, c + 1)): while l >= 0 and r < len(s) and s[l] == s[r]: l -= 1; r += 1 if r - l - 1 > len(best): best = s[l + 1:r] return best print(longest_pal("babad"), longest_pal("cbbd")) # bab bbjavaclass Solution { static String longestPal(String s) { int bs = 0, be = 0; for (int c = 0; c < s.length(); c++) { for (int k = 0; k < 2; k++) { int l = c, r = c + k; while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) { l--; r++; } if (r - l - 1 > be - bs) { bs = l + 1; be = r; } } } return s.substring(bs, be); } }11 Print the characters of a string with their frequency, in first-appearance order
easyCount with a dictionary that keeps insertion order (a LinkedHashMap in Java), then print each character once with its count. Complexity: O(n) time, O(k) space for k distinct characters. Common mistakes: using a plain HashMap in Java, which loses order, and counting spaces unintentionally.
pythonfrom collections import Counter def freq(s): return ''.join(f"{ch}{n}" for ch, n in Counter(s).items()) print(freq("aabbbc")) # a2b3c1javaimport java.util.*; class Solution { static String freq(String s) { Map<Character, Integer> m = new LinkedHashMap<>(); for (char c : s.toCharArray()) m.merge(c, 1, Integer::sum); StringBuilder sb = new StringBuilder(); m.forEach((k, v) -> sb.append(k).append(v)); return sb.toString(); } }
Frequently asked questions
How many coding questions does the TCS NQT have?
TCS publishes the component (advanced IT programming) but not a fixed question count; reports vary by cycle, so read your own test instructions.
Which language is best for the TCS coding round?
Use the one you can debug fastest. Python is shortest to write; Java is safer if you are comfortable with its input handling.
Is CodeVita harder than the NQT coding section?
CodeVita is a separate contest and is commonly described as harder, with longer problem statements; see TCS's own CodeVita page for details.
Do partial test cases give marks?
Many platforms score by test cases passed, so submit a correct brute force before optimising.
Sources
Related guides
More in Company Interview Questions
- TCS Interview Questions for Freshers
- Tech Mahindra Coding Round Questions with Solutions
- Tech Mahindra Interview Questions for Freshers
- Texas Instruments Coding Round Questions with Solutions
- Texas Instruments Interview Questions and Answers for India Embedded Roles
- Thoughtworks Coding Round Questions
- Thoughtworks Interview Questions with Answers
- Tiger Analytics Coding Round Questions
- Tiger Analytics Interview Questions with Answers
- Tower Research Capital Interview Questions and Answers
- Tower Research Coding Round Questions With Solutions
- Uber Coding Round Questions with Solutions in Python and Java