# MindTree Coding Questions and Answers 2023 | Coding Questions Asked in Mindtree

## MindTree Coding Questions and Answers 2023 | Mindtree Coding Solutions | Mindtree Coding Questions in C | Mindtree Coding Questions in Java | Mindtree Coding Questions in C++ | Mindtree Coding and Interview Questions

MindTree Coding Questions and Answers 2023 | Mindtree Actual Coding Questions: All fundamental programming ideas, including Dynamic Programming, data structures, and algorithms, are mixed together in the MindTree Coding Questions. You will be given two questions on the following subjects, including array, string, and common mathematics, in the test. A minimum of one execution of the program is required. Any number of times can be used to run the application. For the final scoring, the execution with the highest score is taken into account. Use “Run custom Input” or “Run code” to examine the output when debugging the code. You must disable debug mode, also known as Custom Input, in order to submit the code. You may use Java, C++, and C to program. Even though languages, if not explicitly stated, are version independent. To get more private jobs, click here

### MindTree Coding Test 2023 Pattern

For better clarity on the upcoming MindTree Coding exams, applicants should refer to the Exam pattern & Syllabus. Knowing the MindTree Syllabus also eases the preparation by defining the topics on which a candidate can be tested. Understanding the MindTree exam pattern is of utmost importance to know the scheme of the examination and to prepare accordingly.

Activity Information
Total Questions 02
Duration 45 Minutes
Difficulty High

## Coding Questions Asked in Mindtree

#### Easy:

• Find GCD and LCM of two numbers.
• Program to check whether the given number is prime or not.
• To check whether the number is Armstrong.
• Program to check number is strong or not.
• Check whether the given number is Automorphic or not.
• To print the Fibonacci series up to the n value.
• Prime number printing within a given range.

#### Medium:

• Pyramid pattern printing program.
• Reversing a number.
• Printing the Armstrong numbers between the given intervals.
• Converting Binary number to Decimal and vice versa.
• Decimal to Octal conversion.
• Binary to octal conversion.

#### Hard:

• Program for Palindrome pattern printing.
• Removing the vowels from the string.
• Finding the frequency of the characters from the given string.
• Program to find roots of the quadratic equation.
• Diamond printing pattern programs.
• Finding the largest palindrome number from the given array.

### Pattern Programs

C program to print pyramid star patterns:

In this program, we will ask the user how many star pattern you want. Then, the user will type the integer and the compiler will start printing the star patterns on the screen.

//Learnprogramo
#include<stdio.h>
int main()
{
int row, c, n, temp;
printf(“Enter the number of rows in pyramid of stars you wish to see “);
scanf(“%d”,&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
/* for spacing purpose */
for ( c = 1 ; c < temp ; c++ )
printf(” “);
temp–;
/* for printing stars */
for ( c = 1 ; c <= 2*row – 1 ; c++ )
printf(“*”);
printf(“\n”);
}
}

### Output: C program to print Pascal Number Triangle:

//Learnprogramo
#include<stdio.h>
long factorial(int);
int main()
{
int row, c, n, temp;
printf(“Enter the number of rows you wish to see in pascal triangle \n”);
scanf(“%d”,&n);
temp = n;
for ( row = 0 ; row < n ; row++ )
{
/* for spacing purpose */
for ( c = 1 ; c < temp ; c++ )
printf(” ” );
temp–;
/* for printing stars */
for ( c = 0 ; c <= row ; c++ )
printf(“%ld “,factorial(row)/(factorial(c)*factorial(row-c)));
printf(“\n”);
}
}
long factorial(int n)
{
int c;
long int result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}

### Output: C program to print Floyd’s Triangle:

//Learnprogramo
#include<stdio.h>
int main()
{
int n, row, c, a = 1;
printf(“Enter the number of rows of Floyd’s triangle to print\n”);
scanf(“%d”, &n);
for (row = 1; row <= n; row++)
{
for (c = 1; c <= row; c++)
{
printf(“%d “,a);
a++;
}
printf(“\n”);
}
}

## MindTree Coding Questions and Solutions in C

Q) Build a program for calculating and returning the sums of absolute differences between adjacent numbers in arrays of positive integers. This must be calculated from the position determined by the current user.

In the case of this coding problem, you use three positional arguments through a findTotalSum function. The three inputs you would require are the number of elements inside the array, the elements in the array and the position from where this function will take place.

For example, suppose the total number of elements is 5 and these are the elements:

1 2 3 6 4

Then, if we decide to start from the 3rd position or enter 3 as input, the function will occur from ‘3’, the 3rd number in the array.

Hence, the sum would be a total of (6-3)+(4-6)= 5

Here is the code:

``````#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;
}``````

Q) Build a program that will allow you to find out how many clothing pieces in total of a certain length can be extracted from a particular number of cloth pieces. We can take the required length for each clothing piece as 10 feet.

In this case, we must first decide upon the length unit as feet and determine the inputs we need. For this function, we will need two inputs, first the number of pieces ( in the array) and the size of each piece in feet inside the array.

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

For example, suppose the total number of elements is 3 and these are the elements:

0 10 40

Then, the first input is 3 followed by the second input of 0, 10 and 40.

Hence, the sum would be a total of 0 + (10/10) + (40/10) = 5

Thus, there could be 5 pieces of clothing extracted from these 3 pieces of cloth of variable sizes.

Here is the code:

``````#include <stdio.h>
int findTotalPieces(int n, int arr[])
{
int feet, total = 0;
for(int i=0; i<n; i++)
{
feet = arr[i] / 10;
total = total + feet;
}
}
int main()
{
int n;
scanf("%d",&n);
int array[n];
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int result = findTotalPieces(n, array);
printf("%d",result);
return 0;
}``````

Q) 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

Here is the code:

```#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;
}```

Q) 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

Here is the code:

```#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;
}```

Q) 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

Here is the code:

```#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;
}```

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

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

Here is the code:

```#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;
}```

## MindTree Coding Questions and Solutions 2023

Question: Airport Authority

Problem Statement -:

In an airport , the Airport  authority decides to charge some minimum amount to the passengers who are carrying luggage with them. They set a threshold weight value, say T, if the luggage exceeds the weight threshold you should pay double the base amount. If it is less than or equal to threshold then you have to pay \$1.

Function Description:

Complete the weightMachine function in the editor below. It has the following parameter(s):

Parameters:

 Name Type Description N Integer number of luggage T Integer weight of each luggage weights[ ] Integer array threshold weight

Returns: The function must return an INTEGER denoting the required amount to be paid.

Constraints:

• 1 <= N <= 10^5
• 1 <= weights[i] <= 10^5
• 1 <= T <= 10^5

Input Format for Custom Testing:

• The first line contains an integer, N, denoting the number of luggages.
• Each line i of the N subsequent lines (where 0 <= i <n) contains an integer describing weight of ith luggage.
• The next line contains an integer, T, denoting the threshold weight of the boundary wall.

Sample Cases:

• Sample Input 1
4
1
2
3
4
3
• Sample Output 1
5
• Explanation:
Here all weights are less than threshold weight except the luggage with weight 4 (at index 3) so all pays base fare and it pays double fare.

C PROGRAM

```#include <stdio.h>
long int weightMachine(long int N,long int weights[],long int T)
{
long int amount=0,i;
for(i=0;i<N;i++)
{
amount++;
if(weights[i]>T)
{
amount++;
}
}
return amount;
}
int main()
{
long int N,i,T;
scanf("%ld",&N);
long int weights[N];
for(i=0;i<N;i++)
{
scanf("%ld",&weights[i]);
}
scanf("%ld",&T);
printf("%ld",weightMachine(N,weights,T));

return 0;
}```

PYTHON PROGRAM

```def weightMachine(N,weights,T):
amount=0
for i in weights:
amount+=1
if(i>T):
amount+=1
return amount
N=int(input())
weights=[]
for i in range(N):
weights.append(int(input()))
T=int(input())
print(weightMachine(N,weights,T))```

Question: Parallel Columbus

Problem Statement: Nobel Prize-winning Austrian-Irish physicist Erwin Schrödinger developed a machine and brought as many Christopher Columbus from as many parallel universes he could. Actually he was quite amused by the fact that Columbus tried to find India and got America. He planned to dig it further.

Though totally for research purposes, he made a grid of size n X m, and planted some people of America in a position (x,y) [in 1 based indexing of the grid], and then planted you with some of your friends in the (n,m) position of the grid. Now he gathered all the Columbus in 1,1 positions and started a race.

Given the values for n, m, x, y, you have to tell how many different Columbus(s) together will explore you as India for the first time.

Remember, the Columbus who will reach to the people of America, will be thinking that as India and hence wont come further.

Function Description:

Complete the markgame function in the editor below. It has the following parameter(s):

Parameters:

 Name Type Description n Integer The number of rows in the grid. m Integer The number of columns in the grid. x Integer The American cell’s Row. y Integer The American cell’s Column.

Constraints:

• 1 <= n <= 10^2
• 1 <= m <= 10^2
• 1 <= x <= n
• 1 <= y <= m

Input Format:

• The first line contains an integer, n, denoting the number of rows in the grid.
• The next line contains an integer m, denoting the number of columns in the grid.
• The next line contains an integer, x, denoting the American cell’s row.
• The next line contains an integer, y, denoting the American cell’s column.

Sample Cases

Sample Input 1

2

2

2

1

Sample Output 1

1

Explanation

The only way possible is (1,1) ->(2,1) -> (2,2), so the answer is 1.

C++ PROGRAM

```#include <bits/stdc++.h>
using namespace std;
unordered_map<int,long long int> f;

long long int Fact(int n)
{
if(f[n]) return f[n];
return f[n]=n*Fact(n-1);
}

int main()
{
int n,m,x,y;
cin>>n>>m>>x>>y;
n-=1;m-=1;x-=1;y-=1;
f=f=1;
int p=(Fact(m+n)/(Fact(m)*Fact(n)));
int imp=((Fact(x+y)/(Fact(x)*Fact(y)))*(Fact(m-x+n-y)/(Fact(m-x)*Fact(n-y))));
cout<<p-imp;
}```

PYTHON PROGRAM

```import math
n=int(input())-1
m=int(input())-1
x=int(input())-1
y=int(input())-1

ans=math.factorial(n+m)
ans=ans//(math.factorial(n))
ans=ans//(math.factorial(m))

ans1=math.factorial(x+y)
ans1=ans1//(math.factorial(x))
ans1=ans1//(math.factorial(y))

x1=n-x
y1=m-y

ans2=math.factorial(x1+y1)
ans2=ans2//(math.factorial(x1))
ans2=ans2//(math.factorial(y1))
print(ans-(ans1*ans2))```

Question: Amusement Park

Problem Statement: Aashay loves to go to WONDERLA , an amusement park. They are offering students who can code well with some discount. Our task is to reduce the cost of the ticket as low as possible.

They will give some k turns to remove the cost of one ticket where the cost of tickets are combined and given as string. Help Aashay in coding as he is not good in programming and get a 50%  discount for your ticket.

Constraints:

• 1 <= number of tickets <= 10^5
• 1 <= K <= number of tickets

Input Format for Custom Testing:

• The first line contains a string,Tickets, denoting the given cost of each ticket.
• The next line contains an integer, K, denoting the number of tickets that is to be removed.

Sample Cases:

• Sample Input 1
203
3
• Sample Output 1
0

C++ PROGRAM

```#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int smallestNumber (string num, int k)
{
if(num.length()<=k)
return 0;
unordered_map<char,int> pos;
for(int i=0;i<num.length();i++)
{ pos[num[i]]=i;}

string temp=num;
sort(num.begin(),num.end());
string ans=num.substr(0,num.length()-k);
vector<int> v;
for(int i=0;i<ans.length();i++)
v.push_back(pos[ans[i]]);

sort(v.begin(),v.end());
string ret;
for(int i=0;i<v.size();i++)
{ret+=temp[v[i]];}
int final=stoi(ret);

return final;
}
int main()
{
string s;
cin >> s;
int k;
cin >> k;
int ans;
cout<<smallestNumber(s,k)%(int)(pow(10,9)+7);
return 0;
}```

PYTHON PROGRAM

```import sys
n=input()
k=int(input())
n1=len(n)
if len(n)<=k:
print(0)
sys.exit()

a=''
i=0
while i<(n1-1) and k>0:
if int(n[i])>int(n[i+1]):
i+=1
k-=1
continue

else:
a+=n[i]
i+=1

a+=n[i]
i+=1
if k>0:
a=a[:-k]

if i<=(n1-1):
while i<n1:
a+=n[i]
i+=1

print(int(a)%((10**9)+7))```

Question: Self Sufficient

Problem Statement: Abhijeet is one of those students who tries to get his own money by part time jobs in various places to fill up the expenses for buying books. He is not placed in one place, so what he does, he tries to allocate how much the book he needs will cost, and then work to earn that much money only. He works and then buys the book respectively. Sometimes he gets more money than he needs so the money is saved for the next book. Sometimes he doesn’t. In that time, if he has stored money from previous books, he can afford it, otherwise he needs money from his parents.

Now His parents go to work and he can’t contact them amid a day. You are his friend, and you have to find how much money minimum he can borrow from his parents so that he can buy all the books.

He can Buy the book in any order.

Function Description:

Complete the function with the following parameters:

 Name Type Description N Integer How many Books he has to buy that day. EarnArray[ ] Integer array Array of his earnings for the ith book CostArray[ ] Integer array Array of the actual cost of the ith book.

Constraints:

• 1 <= N <= 10^3
• 1 <= EarnArray[i] <= 10^3
• 1 <=  CostArray[i] <= 10^3

Input Format:

• First line contains N.
• Second N lines contain The ith earning for the ith book.
• After that N lines contain The cost of the ith book.

Output Format: The minimum money he needs to cover the total expense.

Sample Input 1:

3

[3 4 2]

[5 3 4]

Sample Output 1:

3

Explanation:

At first he buys the 2nd book, which costs 3 rupees, so he saves 1 rupee. Then he buys the 1st book, that takes 2 rupees more. So he spends his stored 1 rupee and hence he needs 1 rupee more. Then he buys the last book.

C++ PROGRAM

```#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, ans =0,sum=0;
cin>>n;
vector<int> arr1(n),arr2(n);
for(int i=0;i<n;i++) cin>>arr2[i];
for(int i=0;i<n;i++) cin>>arr1[i];
for(int i=0;i<n;i++) arr2[i]-=arr1[i];

sort(arr2.begin(),arr2.end(),greater<int>());
for(int i=0;i<n;i++)
{
sum+=arr2[i];
if(sum<0)
{ans+=abs(sum);sum=0;}
}
cout<<ans;
}```

PYTHON PROGRAM

```n=int(input())
L1= *n
L2=*n
for i in range(n):
L2[i]=int(input())
for i in range(n):
L1[i]=int(input())

for i in range(n):
L2[i]=L2[i]-L1[i];

L2.sort()
su=0
ans=0
for i in range(n):
su=su+L2[i]
if su<0:
ans = ans + abs(su)
su=0

print(ans)```

***BEST OFF LUCK***