Tech Mahindra Menu9>
- Previous Papers
- Tech Mahindra Syllabus
- Quantitative Aptitude
- Verbal English
- Logical Ability
- English Essay
- Conversational Assessment
- Computer Science
- Computer Programming
- Super Coder
- Coding
- Tech Test
- Personality Test
- Tech Mahindra Eligibility Criteria
- Tech Mahindra Recruitment Process
- Interview Questions
PREPINSTA PRIME
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–
- C
- C++
- Java
- 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 Coding | Suggested Avg. Time | Difficulty |
---|---|---|
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
#include <stdio.h>
int countOddEvenDifference(int n, int arr[])
{
int odd = 0, even = 0;
for(int i=0; i<n; i++)
{
if(arr[i]%2==0)
{
even = even+1;
}
else
{
odd = odd+1;
}
}
return odd - even;
}
int main()
{
int n;
scanf("%d",&n);
int array[n];
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int result = countOddEvenDifference(n, array);
printf("%d",result);
return 0;
}
def countOddEvenDifference(n,numbers): total=0
for i in numbers: if i%2 == 0: total = total - 1 else: total = total + 1 return total n = int(input()) numbers = list(map(int,input().split())) print(countOddEvenDifference(n,numbers))
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
#include <stdio.h> #include <stdlib.h> int findTotalSum(int n, int arr[], int start) { int difference, sum=0; for(int i=start-1; i<n-1; i++) { difference = abs(arr[i]-arr[i+1]); sum = sum + difference; } return sum; } int main() { int n; int start; scanf("%d",&n); int array[n]; for(int i=0; i<n; i++) { scanf("%d",&array[i]); } scanf("%d",&start); int result = findTotalSum(n, array, start); printf("\n%d",result); return 0; }
def findTotalSum(n,numbers,pos): total = 0 for i in range(pos-1,n-1): total+= abs(numbers[i]-numbers[i+1]) return total n = int(input()) numbers = list(map(int, input().split())) pos = int(input()) print(findTotalSum(n,numbers,pos))
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
#include <stdio.h> int findDifference(int n, int arr[]) { int odd=0, even=0; for(int i=0; i<n; i++) { if(i%2==0) { even = even + arr[i]; } else { odd = odd + arr[i]; } } return even-odd; } int main() { int n; scanf("%d",&n); int array[n]; for(int i=0; i<n; i++) { scanf("%d",&array[i]); } int result = findDifference(n, array); printf("%d",result); return 0; }
def findDifference(n,values): total = 0 for i in range(n): if i%2 == 0: total+=values[i] else: total-=values[i] return total n = int(input()) values = list(map(int, input().split())) print(findDifference(n,values))
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
#include <stdio.h> int findTotalCurtains(int n, int arr[]) { int feet, total = 0; for(int i=0; i<n; i++) { feet = arr[i] / 12; total = total + feet; } return total; } int main() { int n; scanf("%d",&n); int array[n]; for(int i=0; i<n; i++) { scanf("%d",&array[i]); } int result = findTotalCurtains(n, array); printf("%d",result); return 0;
}
def findTotalCurtains(n,numbers): total = 0 for i in numbers: total += i//12 return total n = int(input()) numbers = list(map(int, input().split())) print(findTotalCurtains(n,numbers))
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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)
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);
}
}
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))
can u explain the line 2 code
Hey there,
Kindly join our Discord server for any technical related queries.
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]))
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)
5th python solution:
def findTotalCurtains(arr, n):
feet, total = 0, 0
for i in range(n):
feet = arr[i]//12
total += feet
return total
arr = list(map(int, input().split()))
n = len(arr)
print(findTotalCurtains(arr, n))
please provide codes in python