Cracking the coding interview | Step-7



DAY 7: coding interview questions

Question 1:

Read second word and change to Uppercase: Write a function (method) that takes as input a string (sentence), and returns its second word in uppercase.
For example -
If input1 is “Wipro Technologies Bangalore”,
the function should return "TECHNOLOGIES”
If input1 is “Hello World”,
the function should return "WORLD”
If  input1 is “Championship 2017 League”,
the function should return "2017”
If input1 is “Hello”,
the function should return "LESS”
NOTE 1: If Input1 is a sentence with less than 2 words, the function should return the word “LESS”.
NOTE 2: The result should have no leading or trailing spaces

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode{

public String secondWordUpperCase(String input1){

String strArray[] = input1.split(" ");        

String stt="LESS";        

if(strArray.length==1) {            

return stt;        

}        

return strArray[1].toUpperCase();   

 }}

Question 2: 

isPalindrome

Write a function (method) to determine whether the input string is a Palindrome or not.
What is a Palindrome?
A palindrome is a string that spells the same from either directions, for example - abba,  appa, amma, malayalam, nayan, deed, level, madam, rotator, reviver, stats, tenet, ...
If the input string is a palindrome, the function should return 2
If the input string is NOT a palindrome, the method should return 1
NOTE: The case of the letters in the string should not matter, i.e. Madam , MAdam , madAM , madam , MADAM , should all be considered a palindrome.
ASSUMPTIONS: Within the scope of this assessment, you can assume the following, and so you do not have to write code to handle the below conditions -
The passed input string will always be a single word and not a sentence
The passed input string will only contain alphabets

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
public int isPalindrome(String input1){
String str=input1.toLowerCase();
        String str1="";
        for(int i=str.length()-1;i>=0;i--)
        {
            str1+=str.charAt(i);
        }
        if(str.equals(str1))
        return 2;
        else
        return 1;
    }
}

Question 3: 

Weight of String: Write a function that takes a string as input and calculates the weight of the string as per rules mentioned below.

For calculating the weight of the string,
Weight of all alphabetic characters that appear in the string should be added
Weight of vowels that appear in the string should either be ignored OR added depending upon a specified option
All non-alphabetic characters in the string should be ignored
Weight of each letter is its position in the English alphabet system, i.e. weight of a=1, weight of b=2, weight of c=3, weight of d=4, and so on….weight of y=25, weight of z=26.
Weight of Upper-Case and Lower-Case letters should be taken as the same, i.e. weight of A=a=1, weight of B=b=2, weight of C=c=3, and so on…weight of Z=z=26.
Example1:
Let us assume the word is “Hello World!!” and vowels are to be ignored.
Weight of “Hello World!!” = 8+0+12+12+0+0+23+0+18+12+4+0+0 = 89
Note: Note that weight of vowels is ignored. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.
Example2:
Let us assume the word is “Hello World” and vowels are to be included.
Weight of “Hello World!!” = 8+5+12+12+15+0+23+15+18+12+4+0+0 = 124
Note: Note that weight of vowels is included. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.
The function will accept two input parameters  input1 and input2, 
where,
input1 represents the string whose weight needs to be calculated, and,
input2 represents the option specifying whether or not the weight of vowels should be included.
If input2 is 0, vowels that appear in the string should be ignored.
If input2 is 1, weight of vowels that appear in the string should also be added.
The function is expected to calculate and return the weight of the string.
Example1:
input1 = “Hello World”
input2 = 0
output1 = 89
Example2:
input1 = “Hello World”
input2 = 1
output1 = 124
Example3:
input1 = "All Zebras are Black & White!!"
input2 = 0
output1 = 186​
Example4:
input1 = "All Zebras are Black & White!!"
input2 = 1
output1 = 214

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
public int weightOfString(String input1,int input2)
{
String str=input1.toUpperCase();
        int sum=0;
        for(int i=0;i<input1.length();i++)
        {
          if(input2==0)
          {
      if(str.charAt(i)=='A' || str.charAt(i)=='E' ||str.charAt(i)=='I' || str.charAt(i)=='O' ||                     str.charAt(i)=='U' || !Character.isLetter(str.charAt(i)))
              {
                  continue;
              }
              else
              {
                  int a=str.charAt(i)-64;
                  sum+=a;
              }
          }
          else
          {
              if(!Character.isLetter(str.charAt(i)))
                  continue;
              else
              {
                  int a=str.charAt(i)-64;
                  sum+=a;
              }
          }
        }
        return sum;
    }
}

Question 4: 

Most Frequent Digit – You need to find which digit occurs most number of times across the four given input numbers.

The prototype of the function is:
int MostFrequentDigit (int input1, int input2, int input3, int input4);
where,
input1,  input2, input3 and input4 are the four given input numbers.
The function is expected to find and return the most frequent digit.
Example1 –
If input1=123, input2=234, input3=345, input4=673
We see that across these four numbers,
1, 5, 6 and 7 occur once,
2 and 4 occur twice, and
3 occurs four times.
Therefore, 3 is the most frequent digit and so the function must return 3
NOTE: If more than a digit occurs the same number of most times, then the highest of those digits should be the result. Below example illustrates this.
Example2 –
If input1=123, input2=456, input3=345, input4=5043
We see that
0, 1, 2 and 6 occur once, and
3, 4 and 5 occur thrice.
As there are three digits (3, 4 and 5) that occur most number of times, the result will be the highest (max) digit out of these three. Hence, the result should be 5
Let us see couple of more examples -
Example3 –  
If input1=1203, input2=7624, input3=2046, and input4=1052
The digit 2 occurs four times.
Therefore, the most frequent digit = 2
Example4 – 
If input1=1203, input2=7624, input3=2046, and input4=1002
The digits 0 and 2 occurs four times. 2 is higher than 0.
Therefore, the most frequent digit = 2

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
public int MostFrequentDigit(int input1, int input2, int input3, int input4){
int arr[]={input1,input2,input3,input4};
        int temp[]=new int[10];
        int num;
        for(int i=0;i<arr.length;i++)
        {   num=arr[i];
            while(num!=0)
            {
                int n=num%10;
                temp[n]++;
                num/=10;
            }
        }
        int max=-1;
        int x=0;
        for(int i=0;i<temp.length;i++)
        {
            if(temp[i]>=max)
            {
                max=temp[i];
                x=i;
            }
        }
    return x;
    }
}

Post a Comment

1 Comments

  1. In question 4, code one test case faile. We have to write code for, if any input out of 4 is 0..

    ReplyDelete

Please do not Enter any spam link in the comment box