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 Question

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

14 comments on “Tech Mahindra Coding Question and Programming Paper”


  • gautamsinghgkk

    ANSWER of 1st QUESTION:
    ————-CODE———————
    import java.util.*;
    public class Q1 {
    public static int counOddEvenDiff(int n, int num[]){
    int evenCount=0;
    int oddCount=0;

    for(int i=0;i<n;i++){
    if(num[i]%2==0){
    evenCount += 1;
    }
    else{
    oddCount += 1;
    }
    }
    int difference=oddCount-evenCount;
    return difference;
    }
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter number : ");
    int n=sc.nextInt();

    int num[]=new int[n];
    System.out.print("Enter array number : ");
    for(int i=0;i<n;i++){
    num[i]=sc.nextInt();
    }
    System.out.println(counOddEvenDiff(n, num));
    }
    }

    ANSWER of 2nd QUESTION:
    ————-CODE———————
    import java.util.*;
    public class Q2 {
    public static int findTotalSum(int n, int num[], int position){
    int sum=0;
    position -= 1;

    for(int i=position;i<n-1;i++){
    int difference=num[i+1] – num[i];
    sum += Math.abs(difference);
    }
    return sum;
    }
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter number : ");
    int n=sc.nextInt();

    int num[]=new int[n];
    System.out.print("Enter Array number : ");
    for(int i=0;i<n;i++){
    num[i]=sc.nextInt();
    }
    System.out.print("Enter position : ");
    int position=sc.nextInt();

    System.out.println(findTotalSum(n, num, position));
    }
    }

    ANSWER of 3rd QUESTION:
    ————-CODE———————
    import java.util.*;
    public class Q3 {
    public static int findDiff(int n, int num[]){
    int countEven=0;
    int countOdd=0;
    for(int i=0;i<n;i++){
    if(i%2==0){
    countEven += num[i];
    }
    else{
    countOdd += num[i];
    }
    }
    return countEven-countOdd;
    }
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter number : ");
    int n=sc.nextInt();

    int num[]=new int[n];
    System.out.print("Enter array number : ");
    for(int i=0;i<n;i++){
    num[i]=sc.nextInt();
    }
    System.out.println(findDiff(n, num));
    }
    }

    ANSWER of 4th QUESTION:
    ————-CODE———————
    import java.util.*;
    public class Q4 {
    public static int findTotalCurtain(int n, int num[]){
    int sum=0;

    for(int i=0;i<num.length;i++){
    int value=num[i]/12;
    sum += value;
    }
    return sum;
    }
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter number : ");
    int n=sc.nextInt();

    int num[]=new int[n];
    System.out.print("Enter array number : ");
    for(int i=0;i<n;i++){
    num[i]=sc.nextInt();
    }
    System.out.println(findTotalCurtain(n, num));
    }
    }


  • 4L0- Pavani Latha

    n=int(input())
    l=[]
    l2=[]
    l3=[]
    for i in range(0,n):
    e=int(input())
    l.append(e)
    for i in range(0,n):
    if i%2==0:
    l2.append(l[i])
    else:
    l3.append(l[i])

    print(l2)
    print(l3)
    print(sum(l2)-sum(l3))


  • 4L0- Pavani Latha

    n=int(input())
    l=[]
    for i in range(0,n):
    e=int(input())
    l.append(e)
    print(l)
    k=int(input())
    a=abs(l[k-1]-l[k])
    b=abs(l[k]-l[k+1])
    print(abs((a+b)))


  • 4L0- Pavani Latha

    n=int(input())
    l=[]
    c1=0
    c2=0
    for i in range(0,n):
    ele=int(input())
    l.append(ele)
    print(l)
    for i in l:
    if i%2==0:
    c1+=1
    else:
    c2+=1
    print(c1-c2)


  • Rushikesh

    The codes in java for this set of questions are as follows
    1.package Questions.Array;
    import java.util.*;

    public class Diff_odd_even_elements_arr {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print(“Enter the size of the array: “);
    int n = sc.nextInt();

    int[] array = new int[n];
    System.out.println(“Enter the elements of the array:”);

    for (int i = 0; i < n; i++) {
    System.out.print("Element " + (i + 1) + ": ");
    array[i] = sc.nextInt();
    }

    int oddCount = 0;
    int evenCount = 0;

    for (int i = 0; i =0 && pos < n-1){
    for(int i =pos; i<n-1; i++){
    sum += Math.abs(arr[i] – arr[i+1]);
    }

    }
    else{
    System.out.println("Invalid position");
    }
    return sum;
    }
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter size of array :");
    int n = sc.nextInt();

    int arr[] = new int[n];
    System.out.println("Enter array elements :");

    for (int i = 0; i < n; i++) {
    arr[i] = sc.nextInt();
    }
    System.out.println("Enter the index position from which abs diff is to be determine :");
    int pos = sc.nextInt();

    System.out.println("The absolute diff is:" + find_abs_sum(n,arr,pos));
    }
    }

    3.package Questions.Array;
    import java.util.*;
    public class Even_odd_diff_elem {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter size of array :");
    int n = sc.nextInt();

    int arr[] = new int[n];
    System.out.println("Enter array elements :");

    for (int i = 0; i < n; i++) {
    arr[i] = sc.nextInt();
    }
    int odd_sum=0;
    int even_sum =0;

    for(int i =0; i<n; i++){
    if(arr[i]%2==0){
    even_sum = even_sum + arr[i];
    }
    else{
    odd_sum = odd_sum + arr[i];
    }
    }
    int result = even_sum – odd_sum;
    System.out.println("The difference between even & odd sum elements is :" + result);

    }
    }

    4.package Questions.Array;
    import java.util.*;

    public class curtains_probem_12_feets {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter size of array :");
    int n = sc.nextInt();

    int arr[] = new int[n];
    System.out.println("Enter array elements :");

    for (int i = 0; i < n; i++) {
    arr[i] = sc.nextInt();
    }
    //for(int feet = 0;
    int total_curtains =0;
    for(int i =0; i<n; i++){
    int feet = arr[i]/12;
    total_curtains = total_curtains+feet;
    }
    System.out.println("The total curtains are :" + total_curtains);
    }
    }


  • 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

      return total

      arr = list(map(int, input().split()))
      n = len(arr)

      print(findTotalCurtains(arr, n))