Table of Contents
Tech Mahindra Previous Year Coding Questions and Answers of 2024, 2023 given here. These are repeatedly asked coding questions and answers. So, Practice these coding questions before your actual Tech Mahindra exam within the time frame.
Tech Mahindra Previous Year Coding Questions
Question 1
Given Integer N, we need to find Following :
- Total number of Set Bits in N.
- Position of Least significant set bit.
- Position of Most Significant Set Bit.
The Output should be in String form a#b#c
Where a, b and c are respective 1, 2 and 3 queries.
Example
Input 1 : 10
Output 1: 2#1#3
Explanation 1: Binary form 1 0 1 0
Input 2 : 15
Output 2: 4#0#3
Explanation 2: Binary form 1 1 1 1
[Tech Mahindra Coding Questions 2024]
Question 2
A data compression software utilizes various steps to compress a string of data. One of the steps involves finding the count of characters that are not repeated in the string.
Write an algorithm for the software developer to find the count of characters that are not repeated in the string.
The input consists of a string.
compString representing the string to be compressed.
Print an integer representing the count of characters that are not repeated in the string. If no such character is found or the input string is empty then print 0.
Note
The input string compString is case sensitive. Uppercase characters and lowercase characters are counted as different. The input string compString consists of alphanumeric and special characters only.
Example
Input: alphaadida
Output: 4
Explanation : ** Non repeated characters are l, p ,h ,i **
[Tech Mahindra Coding Questions 2024]
Question 3
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
[Tech Mahindra Coding Questions 2024]
Question 4
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
[Tech Mahindra Coding Questions 2024]
Question 5
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
[Tech Mahindra Coding Questions 2023]