You are on page 1of 8

7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution

olution | Non-SDE Tech & Psychometric Asses…

TECH MAHINDRA Non-SDE ALL CODING


SOLUTIONS:

https://www.youtube.com/c/PappuCareerGuide/

1. Geometric Progression Question with Solution:

Geometric Progression In C++

// Solved By Pappu Career Guide

#include <bits/stdc++.h>

using namespace std;


// Solved By Pappu Kumar (Coder Guy)
int main() {

    double second_term;

    double third_term;

    int n;

    cin>>second_term>>third_term>>n;

    double r = third_term/second_term;  //9/3=3

    double a = second_term/r;

    double nth_term = a * pow( r, n-1);


    cout<<nth_term;

return 0;

}
https://t.me/techmahindra2021crack
In Python

Coming Soon.....

In C

Coming Soon.....

https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 1/8
7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution | Non-SDE Tech & Psychometric Asses…

https://www.youtube.com/c/PappuCareerGuide/

2. BitWise Operation Question with Solution:


In C++
// solved by Pappu Kumar Guide

#include <bits/stdc++.h>

using namespace std;

int main() {

// a#b#c
// ex 
// 10 - binary- 1 0 1 0
// output 2#1#3

int n;

cin>>n;

int a=0; // most

int b=-1; // least

int c=-1;

int i=0;
// solved by Pappu Career Guide

// performing bit marking


while(n>0)

{
    if(n&1)

  {
        a++;

        if(b==-1)

    {

            b=i;
    }

        c=i;
https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 2/8
7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution | Non-SDE Tech & Psychometric Asses…

  }

    i++;

    n=n>>1; //right shift


}

cout<< to_string(a)<<"#" << to_string(b) <<"#" << to_string(c);


// solved by Pappu Kumar Guide
return 0;

In Python

n=int(input())

bi=bin(n)

bi=int(bi[2:])

x=[]

// Solved By Pappu Career Guide

while(bi>0):

    x.append(bi%10)

    bi//=10

a=x.count(1)

b=x.index(1)

x.reverse()

c=len(x)-x.index(1)-1

print(a,b,c,sep="#")

In C
https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 3/8
7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution | Non-SDE Tech & Psychometric Asses…

Coming Soon.....

https://t.me/techmahindra2021crack

3. Frequency Count Question with Solution:

Frequency Count In C++


// Solved By Pappu Career Guide

#include <bits/stdc++.h>

using namespace std;

string helper(string s)

// Solved By Pappu Kumar (Coder Guy)

{
    int arr[26]={0};  // there are 26 alphabets 
in the english char
    for(int i=0;i<s.length();i++) // loop through 
the input string
  {
        arr[s[i]-97]++; // 
to come back with the 0 position

  }
    string result="";

    for(int i=0;i<26;i++)  // for character 


count with loop
  {
        if(arr[i]>0) // if char is present

    {
            char ch = 97+i; // add the value of 
ASCII value of i = 1 - taking b
https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 4/8
7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution | Non-SDE Tech & Psychometric Asses…

            result+=ch;

            result+=to_string(arr[i]);

    }

  }

    return result;
https://t.me/techmahindra2021crack
}

int main() {

    string s;

    cin>>s;

    cout<<helper(s);

    return 0;

In Python

Coming Soon.....

In C

Coming Soon.....

https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 5/8
7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution | Non-SDE Tech & Psychometric Asses…

4. Caesar Cipher Question with Solution:

Caesar Cipher In C++


// Solved By Pappu Career Guide

#include <bits/stdc++.h>
using namespace std;
// Solved By Pappu Kumar (Coder Guy)

string helper(string s)
{
    string result = ""; // answer store string

    for(int i=0;i<s.length();i++) // loop through


 the input string

  {
        char ch = char((s[i] + 3 - 97)%26 + 97);

        result+=ch;

  }

    return result;

}
int main() {

    string s;

    cin>>s;

    cout<<helper(s);

    return 0;

https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 6/8
7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution | Non-SDE Tech & Psychometric Asses…

}
https://t.me/techmahindra2021crack

In C

Coming Soon.....

In Python

Coming Soon.....

5. Number of Decodings

The solution in Python:

def numDecodings(s): 

 if not s:

  return 0

 dp = [0 for x in range(len(s) + 1)] 

 # base case initialization

 dp[0] = 1 
https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 7/8
7/2/22, 11:40 PM Tech Mahindra 2nd Round 12th April Exam Shifts All Sections Question with Solution | Non-SDE Tech & Psychometric Asses…

 dp[1] = 0 if s[0] == "0" else 1   #(1)

 for i in range(2, len(s) + 1): 

  # One step jump

  if 0 < int(s[i-1:i]) <= 9:    #(2)

   dp[i] += dp[i - 1]

  # Two step jump

  if 10 <= int(s[i-2:i]) <= 26: #(3)

   dp[i] += dp[i - 2]

 return dp[len(s)]

https://t.me/techmahindra2021crack

s=input()

print(numDecodings(s))

In C

Coming Soon.....

In c++

Coming Soon.....

https://www.freshersocjob.com/2021/04/tech-mahindra-2nd-round-12th-april-exam.html 8/8

You might also like