Key Areas Covered in Tech Mahindra Coding Questions
Here are the key topics asked in Tech Mahindra coding questions:
- Arrays
- Linked Lists
- Stacks
- Queues
- Trees
- Graphs
- Hash Maps
- Sorting
- Searching
- Recursion
- Dynamic Programming
- Greedy Algorithms
- Pattern Matching
- String Reversal
- Palindrome Checks
- Prime Numbers
- Fibonacci Series
- GCD/LCM Calculations
- Bitwise Operations
- Checking Even/Odd
- Swapping Numbers
Top Coding Questions in Tech Mahindra
1. Write a program to calculate the factorial of a given number.
Example
Input
5
Output
120
C
#include <stdio.h>
int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}
int main() {
int num;
scanf("%d", &num);
printf("%d\n", factorial(num));
return 0;
}
Java
import java.util.Scanner;
public class Factorial {
public static int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(factorial(num));
}
}
Python
def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)
num = int(input())
print(factorial(num))
2. Write a program to reverse a given string.
Example
Input
Hello
Output
olleH
C
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}
int main() {
char str[100];
scanf("%s", str);
reverseString(str);
printf("%s\n", str);
return 0;
}
Java
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(reversed);
}
}
Python
s = input()
print(s[::-1])
3. Write a program to calculate the sum of all elements in an array.
Example
Input
5
1 2 3 4 5
Output
15
C
#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("%d\n", sum);
return 0;
}
Java
import java.util.Scanner;
public class ArraySum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
sum += arr[i];
}
System.out.println(sum);
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
print(sum(arr))
4. Write a program to check if a given number is prime.
Example
Input
17
Output
17 is a prime number
C
#include <stdio.h>
#include <stdbool.h>
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;
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}
Java
import java.util.Scanner;
public class PrimeCheck {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (isPrime(num))
System.out.println(num + " is a prime number");
else
System.out.println(num + " is not a prime number");
}
}
Python
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
num = int(input())
if is_prime(num):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
5. Write a program to print the Fibonacci series up to a given number of terms.
Example
Input
10
Output
0 1 1 2 3 5 8 13 21 34
C
#include <stdio.h>
void fibonacci(int n) {
int first = 0, second = 1, next;
for (int i = 0; i < n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
fibonacci(n);
return 0;
}
Java
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int first = 0, second = 1;
for (int i = 0; i < n; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
System.out.println();
}
}
Python
n = int(input())
first, second = 0, 1
for _ in range(n):
print(first, end=" ")
first, second = second, first + second
print()
6. Write a program to find the maximum element in an array.
Example
Input
5
3 1 4 1 5
Output
5
C
#include <stdio.h>
int main() {
int n, max;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("%d\n", max);
return 0;
}
Java
import java.util.Scanner;
public class MaxInArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println(max);
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
print(max(arr))
7. Write a program to count the number of vowels in a string.
Example
Input
Hello World
Output
3
C
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
scanf(" %[^\n]", str);
int count = 0;
for (int i = 0; str[i]; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
count++;
}
}
printf("%d\n", count);
return 0;
}
Java
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int count = 0;
for (char c : str.toCharArray()) {
if ("aeiouAEIOU".indexOf(c) != -1) {
count++;
}
}
System.out.println(count);
}
}
Python
s = input()
count = sum(1 for char in s if char.lower() in 'aeiou')
print(count)
8. Write a Program to check whether the string is palindrome or not
Example
Input
madam
Output
madam is a palindrome
C
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isPalindrome(char *str) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
return false;
}
}
return true;
}
int main() {
char str[100];
scanf("%s", str);
if (isPalindrome(str))
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
return 0;
}
Java
import java.util.Scanner;
public class PalindromeCheck {
public static boolean isPalindrome(String str) {
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) return false;
left++;
right--;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
if (isPalindrome(str))
System.out.println(str + " is a palindrome");
else
System.out.println(str + " is not a palindrome");
}
}
Python
s = input()
if s == s[::-1]:
print(f"{s} is a palindrome")
else:
print(f"{s} is not a palindrome")
9. Write a program to count occurrences of a specific character in a string.
Example
Input
Hello World
o
Output
2
C
#include <stdio.h>
int main() {
char str[100], ch;
scanf(" %[^\n]", str);
getchar(); // to consume newline
ch = getchar();
int count = 0;
for (int i = 0; str[i]; i++) {
if (str[i] == ch) {
count++;
}
}
printf("%d\n", count);
return 0;
}
Java
import java.util.Scanner;
public class CharCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char ch = sc.next().charAt(0);
int count = 0;
for (char c : str.toCharArray()) {
if (c == ch) count++;
}
System.out.println(count);
}
}
Python
s = input()
ch = input()
print(s.count(ch))
10. Write a program to merge two sorted arrays.
Example
Input
3
1 3 5
4
2 6 8
Output
1 2 3 4 5 6 8
C
#include <stdio.h>
void merge(int arr1[], int n1, int arr2[], int n2) {
int i = 0, j = 0;
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
printf("%d ", arr1[i++]);
} else {
printf("%d ", arr2[j++]);
}
}
while (i < n1) {
printf("%d ", arr1[i++]);
}
while (j < n2) {
printf("%d ", arr2[j++]);
}
}
int main() {
int n1, n2;
scanf("%d", &n1);
int arr1[n1];
for (int i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
scanf("%d", &n2);
int arr2[n2];
for (int i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
merge(arr1, n1, arr2, n2);
printf("\n");
return 0;
}
Java
import java.util.Scanner;
public class MergeSortedArrays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int[] arr1 = new int[n1];
for (int i = 0; i < n1; i++) {
arr1[i] = sc.nextInt();
}
int n2 = sc.nextInt();
int[] arr2 = new int[n2];
for (int i = 0; i < n2; i++) {
arr2[i] = sc.nextInt();
}
int i = 0, j = 0;
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
System.out.print(arr1[i++] + " ");
} else {
System.out.print(arr2[j++] + " ");
}
}
while (i < n1) {
System.out.print(arr1[i++] + " ");
}
while (j < n2) {
System.out.print(arr2[j++] + " ");
}
System.out.println();
}
}
Python
n1 = int(input())
arr1 = list(map(int, input().split()))
n2 = int(input())
arr2 = list(map(int, input().split()))
merged = sorted(arr1 + arr2)
print(*merged)
11. Write a program to remove duplicates from an array.
Example
Input
5
1 2 2 3 4
Output
1 2 3 4
C
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), compare);
int j = 0;
for (int i = 0; i < n; i++) {
if (i == 0 || arr[i] != arr[i - 1]) {
arr[j++] = arr[i];
}
}
for (int i = 0; i < j; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Java
import java.util.Arrays;
import java.util.Scanner;
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int j = 0;
for (int i = 0; i < n; i++) {
if (i == 0 || !arr[i].equals(arr[i - 1])) {
arr[j++] = arr[i];
}
}
for (int i = 0; i < j; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
arr = sorted(set(arr))
print(*arr)
12. Write a program to sort an array in ascending order.
Example
Input
5
5 2 3 1 4
Output
1 2 3 4 5
C
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), compare);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Java
import java.util.Arrays;
import java.util.Scanner;
public class SortArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
print(*arr)
13. Write a program to count the number of vowels and consonants in a given string.
Example
Input
Hello World
Output
Vowels: 3
Consonants: 7
C
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
fgets(str, sizeof(str), stdin);
int vowels = 0, consonants = 0;
for (int i = 0; str[i]; i++) {
if (isalpha(str[i])) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
vowels++;
} else {
consonants++;
}
}
}
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
return 0;
}
Java
import java.util.Scanner;
public class CountVowelsConsonants {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int vowels = 0, consonants = 0;
for (char c : str.toCharArray()) {
if (Character.isLetter(c)) {
if ("aeiouAEIOU".indexOf(c) != -1) {
vowels++;
} else {
consonants++;
}
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
}
Python
s = input()
vowels = sum(1 for char in s if char.lower() in 'aeiou')
consonants = sum(1 for char in s if char.isalpha() and char.lower() not in 'aeiou')
print(f"Vowels: {vowels}")
print(f"Consonants: {consonants}")
14. Write a program to find the largest element in an array.
Example
Input
5
1 2 3 4 5
Output
5
C
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("%d\n", max);
return 0;
}
Java
import java.util.Scanner;
public class LargestElement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println(max);
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
print(max(arr))
15. Write a program to reverse an array.
Example
Input
5
1 2 3 4 5
Output
5 4 3 2 1
C
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Java
import java.util.Scanner;
public class ReverseArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i = n - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
print(*arr[::-1])
Tips for Cracking Tech Mahindra Coding Rounds
To succeed in the Tech Mahindra coding rounds, consider the following tips:
- Practice problems on Lastminute, LeetCode, HackerRank, and CodeSignal to sharpen your coding skills.
- Understand the basics and ensure you have a strong grasp of fundamental data structure and algorithm concepts.
- Participate in mock coding interviews to get familiar with the format of real interviews.
- During coding tests, manage your time wisely. Start with questions you find easier to build confidence.
- Familiarize yourself with Tech Mahindra coding test questions from previous years.
Conclusion
In conclusion, preparing for Tech Mahindra coding questions requires a strategic approach, focusing on the right topics, and practicing diligently. Familiarity with common coding challenges and effective problem-solving techniques will be beneficial.
Prepare Important Coding questions and Crack Your Dream Job Today
Explore ProgramFrequently Asked Questions
1. Are there specific coding questions asked in the second round of the Tech Mahindra Interview?
Yes, Tech Mahindra's second-round coding questions may include more advanced problem-solving tasks, often requiring candidates to optimize their solutions.
2. Are there any recommended resources for Tech Mahindra coding questions and answers?
Yes, several online platforms such as Lastminute, HackerRank, and LeetCode provide practice questions and detailed explanations that can help in preparing for Tech Mahindra coding interviews.