onlinestudy4u 0 Comments

Tech Mahindra Coding Problem And Solutions | Super Coder Role

Tech Mahindra Coding Problem 2024 And Solutions Asked In Super Coder Profile For 5.5 LPA Package

Tech Mahindra Coding Problem

Problem Statement 1 – Tech Mahindra Coding Problem

Jom runs a juice shop where he has two machines, M and N, for making juice:

  • Machine M is a large machine that can serve juice to 100 customers in one go, consuming X units of power.
  • Machine N is a smaller machine that can serve juice to 4 customers at a time, consuming Y units of power.

Given the number of customers ( P ), you need to determine the minimum number of units of power Jom’s machines will consume to serve exactly ( P ) customers, using any combination of machines M and N.

Input Tech Mahindra Coding Problem

  1. An integer ( P ) representing the number of customers.
  2. An integer ( X ) representing the power consumption of machine M.
  3. An integer ( Y ) representing the power consumption of machine N.

Output

Return an integer value representing the minimum units of power Jom will use to serve ( P ) customers.

Example

Input:

P = 9
X = 40
Y = 8

Output:

24

Explanation: Tech Mahindra Coding Problem

  • Using machine M: It will consume 40 units of power to serve all 9 customers.
  • Using machine N: It will need to run 3 times to serve 12 customers (since 9 customers can be served in 3 runs, covering 4 customers each run). This will consume ( 3 \times 8 = 24 ) units of power.
  • The minimum power consumption is 24 units.

Solution Explanation

To solve this problem, follow these steps:

Calculate Power Consumption Using Machine M:

  • Machine M serves all customers in one go and consumes ( X ) units of power.

Calculate Power Consumption Using Machine N:

  • Determine how many times machine N needs to run to serve all ( P ) customers.
  • If ( P \% 4 == 0 ), it means the number of customers is exactly divisible by 4, so ( P / 4 ) runs are needed.
  • If ( P \% 4 \neq 0 ), an additional run is needed to cover the remaining customers, so ( P // 4 + 1 ) runs are needed.
  • Multiply the number of runs by ( Y ) to get the total power consumption using machine N.

Compare and Return the Minimum Power Consumption:

  • Compare the power consumption calculated for both machines and return the smaller value.

Solution Implementation

Here is the implementation of the solution in Python:

class UserMainCode(object):
    @classmethod
    def minimumUnit(cls, input1, input2, input3):
        P = input1  # Number of customers
        X = input2  # Power consumption of machine M
        Y = input3  # Power consumption of machine N

        # Power consumption using machine M
        power_m = X

        # Power consumption using machine N
        if P % 4 == 0:
            runs_n = P // 4
        else:
            runs_n = P // 4 + 1
        power_n = runs_n * Y

        # Return the minimum power consumption
        return min(power_m, power_n)

# Example usage
print(UserMainCode.minimumUnit(9, 40, 8))  # Output: 24

Explanation of the Code

Input Parsing: Tech Mahindra Coding Problem

  • The inputs ( P ), ( X ), and ( Y ) are parsed from the function arguments.

Power Consumption Calculation:

  • Machine M: Simply uses ( X ) units of power.
  • Machine N: Determines the number of runs required using integer division and modulus operations. If ( P \% 4 == 0 ), it divides perfectly by 4; otherwise, an extra run is needed. The total power consumption is the number of runs multiplied by ( Y ).

Comparison and Output: Tech Mahindra Coding Problem

  • The function returns the smaller of the two calculated power consumption values.

Certainly! Let’s go through the problem again and provide a solution with a clear explanation.

Problem Statement 2 – Tech Mahindra Coding Problem

Your friend gives you a string S which contains the name of a person and asks you to find the first and last letter of their name. Your task is to find and return a string representing the first and the last letter.

Note: Tech Mahindra Coding Problem

  • The output should be two capital letters with a dot separating them.
  • The output is case-sensitive.

Input Specification: Tech Mahindra Coding Problem

  • input1: A string S representing the name.

Output Specification: Tech Mahindra Coding Problem

  • Return a string representing the first and the last letter.

Example:

  • Input: “Sam Harris”
  • Output: “S.S”

Explanation: Tech Mahindra Coding Problem

Here, the name is ‘Sam Harris’. The first letter of the first name is ‘S’, and the last letter of the last name is ‘S’. Hence, S.S is returned as the output.

Solution : Tech Mahindra Coding Problem

To solve this problem, we can follow these steps:

  1. Split the input string into parts using space as a delimiter.
  2. Extract the first letter of the first name and convert it to uppercase.
  3. Extract the last letter of the last name and convert it to uppercase.
  4. Combine these two letters with a dot in between and return the result.

Here is the implementation in Python 3:

class UserMainCode(object):
    @classmethod
    def findFirstlast(cls, input1):
        # Split the name into parts
        name_parts = input1.split()

        # Get the first letter of the first name and convert to uppercase
        first_letter = name_parts[0][0].upper()

        # Get the last letter of the last name and convert to uppercase
        last_letter = name_parts[-1][-1].upper()

        # Combine them with a dot in between
        result = f"{first_letter}.{last_letter}"

        return result

# Example usage:
input_name = "Sam Harris"
print(UserMainCode.findFirstlast(input_name))  # Output: S.S

Explanation of the Code – Tech Mahindra Coding Problem

  1. Splitting the Name:
   name_parts = input1.split()

This splits the input string input1 into a list of words using space as the delimiter. For example, “Sam Harris” becomes ['Sam', 'Harris'].

  1. Extracting the First Letter:
   first_letter = name_parts[0][0].upper()

This extracts the first letter of the first name (name_parts[0][0]) and converts it to uppercase.

  1. Extracting the Last Letter:
   last_letter = name_parts[-1][-1].upper()

This extracts the last letter of the last name (name_parts[-1][-1]) and converts it to uppercase.

  1. Combining the Letters:
   result = f"{first_letter}.{last_letter}"

This formats the two letters into a string separated by a dot.

  1. Returning the Result:
   return result

This solution ensures that both the first and last letters are uppercase and properly formatted according to the given requirements.

Subscribe On Youtube

Home