Tech Mahindra Coding Round Questions with Solutions
Last updated 2026-11-19
Tech Mahindra's online test is reported to have a coding or technical section at an accessible level: strings, numbers, bits and patterns. These ten problems practise that level with Python and Java solutions, complexity and common mistakes.
Pair it with Tech Mahindra interview questions and how to crack the DSA coding round.
| Level | Easy to medium |
|---|---|
| Languages | Python and Java shown |
| Topics | Bits, strings, patterns, arrays |
What Tech Mahindra coding questions look like
Prep sources report that the online test is delivered on an AMCAT or SHL based interface with an aptitude section and a technical or coding section, each with its own cut-off. Reports of the coding part describe straightforward problems on strings, number manipulation, bits and arrays, plus pattern printing. The ten problems below match that level and are practice in the style of those topics, not a recorded paper. Several are also short enough to be asked live in a technical interview.
Practical tips
- Learn bit tricks: n & (n - 1), n & 1, shifts, XOR for pairs.
- Practise pattern printing until spaces and counts feel automatic.
- When a problem says in place, do not allocate a second array.
- Test the empty string and a one-character string.
Tech Mahindra Coding Round Questions with Solutions: questions with worked answers
1 Count set bits in an integer
easyBrian Kernighan's trick: n & (n - 1) clears the lowest set bit, so loop until n is zero and count iterations. This is faster than checking all 32 bits when few are set. Complexity: O(k) time for k set bits, O(1) space. Common mistakes: looping with n >> 1 on negative numbers in Java (use >>>) and counting characters of a binary string when asked for an in-place method.
pythondef set_bits(n): c = 0 while n: n &= n - 1 c += 1 return c print(set_bits(13), set_bits(255)) # 3 8javaclass Solution { static int setBits(int n) { int c = 0; while (n != 0) { n &= n - 1; c++; } return c; } }2 Check whether a number is a power of two
easyA positive power of two has exactly one set bit, so n > 0 and (n & (n - 1)) == 0. Complexity: O(1) time and space. Common mistakes: forgetting n > 0 (zero would pass) and using a floating-point log that can misjudge large values.
pythondef is_pow2(n): return n > 0 and n & (n - 1) == 0 print(is_pow2(64), is_pow2(100), is_pow2(0)) # True False Falsejavaclass Solution { static boolean isPow2(long n) { return n > 0 && (n & (n - 1)) == 0; } }3 Run-length encode a string
easyWalk through the string, counting consecutive equal characters and appending the character plus its count when the run ends. Emit the final run after the loop. Complexity: O(n) time, O(n) space. Common mistakes: dropping the last run and treating a count of 1 inconsistently with the problem statement.
pythondef rle(s): if not s: return "" out, run = [], 1 for i in range(1, len(s) + 1): if i < len(s) and s[i] == s[i - 1]: run += 1 else: out.append(s[i - 1] + str(run)); run = 1 return ''.join(out) print(rle("aaabbc")) # a3b2c1javaclass Solution { static String rle(String s) { StringBuilder sb = new StringBuilder(); int run = 1; for (int i = 1; i <= s.length(); i++) { if (i < s.length() && s.charAt(i) == s.charAt(i - 1)) run++; else { sb.append(s.charAt(i - 1)).append(run); run = 1; } } return sb.toString(); } }4 Remove duplicates from a sorted array in place
easyKeep a write index for the last unique value; copy an element only when it differs from the one at the write index. Return the new length. Complexity: O(n) time, O(1) space. Common mistakes: comparing with the previous read element after it was overwritten and returning the index instead of the length.
pythondef dedupe(a): if not a: return 0 w = 1 for i in range(1, len(a)): if a[i] != a[w - 1]: a[w] = a[i]; w += 1 return w a = [1, 1, 2, 2, 3] print(dedupe(a), a[:3]) # 3 [1, 2, 3]javaclass Solution { static int dedupe(int[] a) { if (a.length == 0) return 0; int w = 1; for (int i = 1; i < a.length; i++) if (a[i] != a[w - 1]) a[w++] = a[i]; return w; } }5 Print the first N rows of Pascal's triangle
easyEach row starts and ends with 1; an inner value is the sum of the two values above it. Build each row from the previous one. Complexity: O(n squared) time and space. Common mistakes: off-by-one in the row length and using factorials, which overflow quickly.
pythondef pascal(n): rows = [] for i in range(n): row = [1] * (i + 1) for j in range(1, i): row[j] = rows[i - 1][j - 1] + rows[i - 1][j] rows.append(row) return rows print(pascal(5)[-1]) # [1, 4, 6, 4, 1]javaimport java.util.*; class Solution { static List<List<Integer>> pascal(int n) { List<List<Integer>> rows = new ArrayList<>(); for (int i = 0; i < n; i++) { List<Integer> row = new ArrayList<>(); for (int j = 0; j <= i; j++) row.add(j == 0 || j == i ? 1 : rows.get(i - 1).get(j - 1) + rows.get(i - 1).get(j)); rows.add(row); } return rows; } }6 Convert a Roman numeral to an integer
mediumScan left to right; if a symbol is smaller than the next one, subtract it, otherwise add it. This handles IV, IX, XL and so on without special cases. Complexity: O(n) time, O(1) space. Common mistakes: adding every value (misses subtractive pairs) and reading past the end when checking the next symbol.
pythondef roman(s): v = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} t = 0 for i, c in enumerate(s): if i + 1 < len(s) and v[c] < v[s[i + 1]]: t -= v[c] else: t += v[c] return t print(roman("MCMXCIV")) # 1994javaimport java.util.*; class Solution { static int roman(String s) { Map<Character, Integer> v = Map.of('I', 1, 'V', 5, 'X', 10, 'L', 50, 'C', 100, 'D', 500, 'M', 1000); int t = 0; for (int i = 0; i < s.length(); i++) { int cur = v.get(s.charAt(i)); if (i + 1 < s.length() && cur < v.get(s.charAt(i + 1))) t -= cur; else t += cur; } return t; } }7 Longest common prefix of an array of strings
easyTake the first string as the prefix and shorten it until each other string starts with it. Stop early if it becomes empty. Complexity: O(total characters) time, O(1) extra space. Common mistakes: not handling an empty array and comparing from the wrong end of the strings.
pythondef lcp(strs): if not strs: return "" p = strs[0] for s in strs[1:]: while not s.startswith(p): p = p[:-1] if not p: return "" return p print(lcp(["flower", "flow", "flight"])) # fljavaclass Solution { static String lcp(String[] strs) { if (strs.length == 0) return ""; String p = strs[0]; for (int i = 1; i < strs.length; i++) { while (!strs[i].startsWith(p)) { p = p.substring(0, p.length() - 1); if (p.isEmpty()) return ""; } } return p; } }8 Add two binary strings
mediumWalk both strings from the end with a carry, adding digits, writing sum mod 2 and carrying sum divided by 2. Reverse the result at the end. Complexity: O(max(m, n)) time, O(max(m, n)) space. Common mistakes: converting to integers (overflow for long strings) and forgetting the final carry.
pythondef add_binary(a, b): i, j, carry, out = len(a) - 1, len(b) - 1, 0, [] while i >= 0 or j >= 0 or carry: s = carry if i >= 0: s += int(a[i]); i -= 1 if j >= 0: s += int(b[j]); j -= 1 out.append(str(s % 2)); carry = s // 2 return ''.join(reversed(out)) print(add_binary("1010", "1011")) # 10101javaclass Solution { static String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); int i = a.length() - 1, j = b.length() - 1, carry = 0; while (i >= 0 || j >= 0 || carry > 0) { int s = carry; if (i >= 0) s += a.charAt(i--) - '0'; if (j >= 0) s += b.charAt(j--) - '0'; sb.append(s % 2); carry = s / 2; } return sb.reverse().toString(); } }9 Leaders in an array
mediumAn element is a leader if it is strictly greater than everything to its right. Scan from the right keeping the maximum seen; each new maximum is a leader. Reverse for original order. Complexity: O(n) time, O(1) extra space. Common mistakes: using an O(n squared) comparison for every element and always counting the last element (it is a leader by definition).
pythondef leaders(a): out, mx = [], float('-inf') for x in reversed(a): if x > mx: out.append(x); mx = x return out[::-1] print(leaders([16, 17, 4, 3, 5, 2])) # [17, 5, 2]javaimport java.util.*; class Solution { static List<Integer> leaders(int[] a) { LinkedList<Integer> out = new LinkedList<>(); int mx = Integer.MIN_VALUE; for (int i = a.length - 1; i >= 0; i--) if (a[i] > mx) { out.addFirst(a[i]); mx = a[i]; } return out; } }10 Print a diamond pattern of stars
mediumFor N, print rows 1 to N with i stars centred, then N-1 down to 1. Each row has N - i leading spaces and 2i - 1 stars. Pattern printing appears in screening tests because it checks nested loops and spacing. Complexity: O(N squared) time, O(1) space. Common mistakes: miscounting spaces so the shape skews, and printing the middle row twice.
pythondef diamond(n): rows = list(range(1, n + 1)) + list(range(n - 1, 0, -1)) return '\n'.join(' ' * (n - i) + '*' * (2 * i - 1) for i in rows) print(diamond(3))javaclass Solution { static void diamond(int n) { for (int k = 1; k < 2 * n; k++) { int i = k <= n ? k : 2 * n - k; System.out.println(" ".repeat(n - i) + "*".repeat(2 * i - 1)); } } }
Frequently asked questions
How hard is Tech Mahindra's coding test?
Commonly reported as easy to medium; the separate sections and cut-offs make time management the challenge.
Do I need bit manipulation?
It appears often enough in screening tests that a few tricks are worth learning.
Which languages are allowed?
Check the platform instructions for your test; Python, Java and C++ are typical.
Is pattern printing still asked?
It is commonly reported in screening tests, so practise a few shapes.
Sources
Related guides
More in Company Interview Questions
- 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
- Uber Interview Questions and Answers for SDE Roles
- UBS coding round questions with solutions