Try new tool: URL Shortener

https://res.cloudinary.com/hg6hswtwo/image/upload/v1/media/pics/tech_mahindra_coding_question_2021_ae59fw
default

Write a program to calculate the.. TechM 2021 coding question





Hello guys, Today in this post I am solving a most efficient coding question which is asked in Tech Mahindra off-campus placement drive in this year.

This test was held on 25 August 2021 in TechM aptitude test.

First let's discuss the question and then we will see the steps to solve this question.

Coding question - Tech Mahindra campus placement drive 2021

Write a program to calculate the total bill tax amount for a list of billing amounts passed as an array of long integers.

Up to the amount 1000, there is no tax applicable, subsequntly, a flat tax of 10% is applicable for the remaining amount as per the tax rate.

Note :

  1. All calculations and results should be integer based ignoring fractions.
  2. You are expected to write code in the calcTotalTax function only which will receive the first parameter as the number of items in the array and second parameter as the array itself.

Lets understand by simple example.

Example :

Calculating total tax for a list of billing amounts

Input :

Input1 : 5

Input2 : 1000 2000 3000 4000 5000

Output :

1000

Explanation :

The first parameter (5) is the size of array. Next is an array of billing amounts. For the first amount there will be 0 tax and for the next amounts it will be 10% of (2000 - 1000) = 100 and so on. The sum of all the tax amounts will be (0+100+200+300+400=1000).

This is all about the coding question they gave.

Now this question you can solve in any programming languages you want but I am solving this in Python. If you know how to solve this problem other languages like JAVA, C, C++, etc then please comment your whole program we will update in this post.

How to solve this coding question?

It is very easy to solve just you need to understand the question carefully and divide it into sub parts. So lets divide this :

Basically I am dividing this question into 3 parts and then make or build a logic to each part.

  1. First dividation is to getting inputs from user which is the size of array and second is billing amounts.
  2. Second part is to appending all the billing amounts taken from user into the list.
  3. And last part is for building a logic to solve tax and add them to get output.

Now build a logic for each of the parts.

First part is to take both inputs from the user. Keep on thing in mind is that inputs is integer type so we have take input as an integer type.

input1 = int(input("Enter the size: "))

input2 = int(input("Enter the billing amount: ")) #but here input2 is list so we not take input like this

#input2 = int(input("Enter the size: "))

#i am using list comprehension here to make input as a list

input2 = [ int(input("Enter the size: ")) ]

cal = 0

total = 0 #assume

Now first part logic is finished now lets solve second part is to append all the inputs from the user. We can take inputs by above statements but what we have to do is we have to append in the list so for that we need for loop.

for i in range(0, input1-1):

     #again taken input

     amt = int(input()) #but this time no input string because we already taken above as input2

     input2.append(amt) # and we taken input in list

print (input2) #check it is appended or not by printing

For third part we have to build some extra logic. Because there will be no tax for 1st billing amount and after that all amounts have 10% of ( 2000 - 1000). So for this again take one more for loop.

#to not take 1st billing amount we have to do slicing like this

for a in input2[2:]:

        cal = ( a - 1000) / 10   # this is how we get the calculations for each billing amounts except 1st one

        total = total + cal   # add all calculations for final output

print (total)

So this is how I build the logic for this and get the output. Overall program is look like this:

input1 = int(input("Enter the size: "))

#i am using list comprehension here to make input as a list

input2 = [ int(input("Enter the billing amount: ")) ]

cal = 0

total = 0 #assume

for i in range(0, input1-1):

     #again taken input

     amt = int(input()) #but this time no input string because we already taken above as input2

     input2.append(amt) # and we taken input in list

#to not take 1st billing amount we have to do slicing like this

for a in input2[1:]:

        cal = ( a - 1000) / 10   # this is how we get the calculations for each billing amounts except 1st one

        total = total + cal   # add all calculations for final output

print (total)

Here is the complete video audio explanation for this question.

Output of program is

Enter the size: 5
Enter the size: 1000
2000
3000
4000
5000
1000.0

Now you can write this program in function.

I hope you understand this problem if not please read it again and try to understand. If you like then don't forget to comment good and as I said earlier if you know how to solve this in other languages then comment your whole program and we will update it.





Publish Your Great Work

Your AD here