# Tech Mahindra Coding Question and Programming Paper

## Tech Mahindra Coding Questions with Answers

Find all the latest Tech Mahindra Coding Questions with Answers here. with these programs, you will get an ample amount of idea for what kind of questions are going to appear in the exam along with their difficulty level and concepts used in that question.

You can use the following Languages to solve the question in the exam–

1. C
2. C++
3. Java
4. Python

## Tech Mahindra Coding and Programming Round Details

There will be 26 Tech Test Question in the Tech Mahindra exam, first question will be the Coding Question and rest all question will be Technical MCQ.
The difficulty level of question is medium to high where they will judge your knowledge of programming although overall time to solve the whole Tech Test Question is 65 minutes. so, you can give approximately 15-20 minutes to coding question in the exam.

Tech Mahindra CodingSuggested Avg. TimeDifficulty
1 Question          15-20 mins Medium

## Sample Tech Mahindra Coding Questions

### Question : 1

Write a program to return the difference between the count of odd numbers and even numbers.

Note : You are expected to write code in the countOddEvenDifference function only which will receive the first parameter as the number of items in the array and second parameter as the array itself. you are not required to take input from the console.

Example
Finding the difference between the count of odd and even numbers from a list of 5  number

Input
input 1 : 8
input 2 : 10 20 30 40 55 66 77 83

Output
-2

Explanation
The first paramter (8) is the szie of the array. Next is an array of integers. The calculation of difference between count sum of odd and even numbers is as follows:

3 (count of odd numbers) – 5 (count of even numbers) = -2

### Question : 2

Write a program to calculate and return the sum of absolute difference between the adjacent number in an array of positive integers from the position entered by the user.

Note : You are expected to write code in the findTotalSum function only which receive three positional arguments:

1st : number of elements in the array
2nd : array
3rd : position from where the sum is to be calculated

Example

Input
input 1 : 7
input 2 : 11 22 12 24 13 26 14
input 3 : 5

Output
25

Explanation

The first parameter 7 is the size of the array. Next is an array of integers and input 5 is the position from where you have to calculate the Total Sum. The output  is 25 as per calculation below.
| 26-13 | = 13
| 14-26 | =  12
Total Sum = 13 + 12 = 25

### Question : 3

Write a program to find the difference between the elements at odd index and even index.

Note : You are expected to write code in the findDifference function only which receive the first parameter as the numbers of items in the array and second parameter as the array itself. You are not required to take the input from the console.

Example

Finding the maximum difference between adjacent items of a list of 5 numbers

Input
input 1 : 7
input 2 : 10 20 30 40 50 60 70

Output
40

Explanation
The first parameter 7 is the size of the array. Sum of element at even index of array is 10 + 30 + 50 + 70 = 160 and sum of elements at odd index of array is 20 + 40 + 60 = 120. The difference between both is 40

### Question : 4

A Cloth merchant has some pieces of cloth of different lengths. He has an order of curtains of length of 12 feet. He has to find how many curtains can  be made from these pieces. Length of pieces of cloth is recorded in feet.

Note : You are expected to write code in the findTotalCurtains function only which receive the first parameter as the number of items in the array and second parameter as the array itself. You are not required to take the input from the console.

Example

Finding the total curtains from a list of 5 cloth pieces.

Input
input 1 : 5
input 2 : 3 42 60 6 14

Output
9

Explanation
The first parameter 5 is the size of the array. Next is an array of measurements in feet. The total number of curtains is 5 which is calculated as under

3 -> 0
42 -> 3
60 -> 5
6 -> 0
14 -> 1
total = 9

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### 9 comments on “Tech Mahindra Coding Question and Programming Paper”

• Saurabh

Codes in Python
Question 1
li = [10, 20, 30, 40, 55, 66, 77, 83]
eve=0
odd=0
for ele in li:
if ele%2==0:
eve+=1
else:
odd+=1
print(odd-eve)

Question 2
li = [11, 22, 12, 24, 13, 26, 14]
pos = 5
sum = abs(li[pos]-li[pos-1])+abs(li[pos]-li[pos+1])
print(sum)

Question 3
li = [10, 20, 30, 40, 50, 60, 70]
l = len(li)
eve = 0
odd = 0
for i in range(0, l, 2):
eve = eve + li[i]
for i in range(1, l, 2):
odd = odd + li[i]
print(eve-odd)

Question 4
li = [3, 42, 60, 6, 14]
sum = 0
for ele in li:
sum = sum + ele//12
print(sum)

• Saurabh

Codes in JAVA
Question 1
public class Practice1 {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 55, 66, 77, 83};
int eve = 0;
int odd = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
eve++;
} else odd++;
}
System.out.println(odd – eve);
}
}

Question 2
public class Practice2 {
public static void main(String[] args) {
int[] arr = {11, 22, 12, 24, 13, 26, 14};
int pos = 5;
int sum = Math.abs(arr[pos] – arr[pos – 1]) + Math.abs(arr[pos] – arr[pos + 1]);
System.out.println(sum);
}
}

Question 3
public class Practice3 {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50, 60, 70};
int eve = 0;
int odd = 0;
for (int i = 0; i < arr.length; i = i + 2) {
eve = eve + arr[i];
}
for (int i = 1; i < arr.length; i = i + 2) {
odd = odd + arr[i];
}
System.out.println(eve-odd);
}
}

Question 4
public class Practice4 {
public static void main(String[] args) {
int sum = 0;
int[] arr = {3, 42, 60, 6, 14};
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i] / 12;
}
System.out.println(sum);
}
}

• Varsha

Question 4 in python:
n=int(input())
k=list(map(int,input().split()))
l=[]
for i in k:
l.append(i//12)
print(sum(l))

• Varsha

Question 2 in python:
n=int(input())
k=list(map(int,input().split()))
i=int(input())
print((k[i]-k[i+1])+(k[i]-k[i-1]))

• Pushkar

Question number 5 code in Python is as Follow
n = int(input())
numbers = list(map(int, input().split()))
numbers.sort()
a=numbers[-1]-numbers[0]
print(a)

• Sai

5th python solution:
def findTotalCurtains(arr, n):
feet, total = 0, 0
for i in range(n):
feet = arr[i]//12
total += feet