Cracking the coding interview | Step-5



DAY 5: coding interview questions

Question 1:

digitSum: The labels on a trader's boxes display a large number (integer). The trader wants to label the boxes with a single digit ranging from 1 to 9. He decides to perform digit sum on this large number, continuously till he gets a single digit number.
NOTE: In mathematics, the "digit sum" of a given integer is the sum of all its digits, (e.g.: the digit sum of 84001 is calculated as 8+4+0+0+1 = 13, the digit sum of 13 is 1+3 = 4).
Write a function (method) that takes as input a large number and returns a single digit by performing continuous digitSum on this number, and on the resulting numbers, till the resulting number is a single digit number in the range 1 to 9.
Example 1: If the large number whose single-digit digitSum is to be found is 976592, the process is as below –
9+7+6+5+9+2 = 38
3+8 = 11
1+1 = 2
Thus, the single-digit digitSum for the number 976592 is 2.
Example 2: If the large number whose single-digit digitSum is to be found is 123456, the process is as below –
1+2+3+4+5+6 = 21
2+1 = 3
Thus, the single-digit digitSum for the number 123456 is 3.
For negative numbers, the result should also be in negative.
Example 3: If the large number whose single-digit digitSum is to be found is -123456, the answer would be -3.

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
                public int digitSum(int input1){
                                int neg=input1;
                                if(input1<0)
                                {
                                                input1*=-1;
                                }
                                int len=Integer.toString(input1).length();
                                if(len==1)
                                {
                                                if(neg<0)
                                                return input1*-1;
                                                else
                                                                return input1;
                                }
                                else
                                {
                                                int sum=0;
                                while(input1!=0)
                                {
                                                int rem=input1%10;
                                                sum+=rem;
                                                input1/=10;
                                }
                                                if(neg<0)
                                                                return digitSum(sum*-1);
                                                else
                                                                return digitSum(sum);
                }}
}


Question 2: 

Even Digits' Sum:

In mathematics, the "digit sum" of a given integer is the sum of all its digits, e.g.
the digit sum of 84001 is calculated as 8+4+0+0+1 = 13,
the digit sum of 158 is 1+5+8 = 14. 
Rohan's teacher has asked him to write a function (method) that takes as input a positive number and performs digitSum of only the even digits in the given number.
Example 1: If the given number is 9625, we must add only the even digits, i.e. 6+2 = 8 Thus, the EvenDigitsSum for the number 9625 is 8.
Example 2: If the given number is 2134, the EvenDigitsSum will be 2+4 = 6
Assumption: The input number will be a positive integer number >= 1 and <= 25000.

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
public int EvenDigitsSum(int inpu1){
int sum=0;
while(input1!=0)
{
int n=input1%10;
if(n%2==0)
sum+=n;
input1/=10; 
}
return sum;
}



Question 3: 

Odd Digits' Sum:

In mathematics, the "digit sum" of a given integer is the sum of all its digits, e.g. 
the digit sum of 84001 is calculated as 8+4+0+0+1 = 13, 
the digit sum of 158 is 1+5+8 = 14. 
Rohan's teacher has asked him to write a function (method) that takes as input a positive number and performs digitSum of only the odd digits in the given number.
Example 1: If the given number is 9625, we must add only the odd digits, i.e. 9+5 = 14 Thus, the OddDigitsSum for the number 9625 is 14.
Example 2: If the given number is 2134, the OddDigitsSum will be 1+3 = 4
Assumption: The input number will be a positive integer number >= 1 and <= 25000.


//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
public int OddDigitsSum(int inpu1){
int sum=0;
while(input1!=0)
{
int n=input1%10;
if(n%2!=0)
sum+=n;
input1/=10; 
}
return sum;
}

Question 4: 

Even OR Odd Digits' Sum:

In mathematics, the "digit sum" of a given integer is the sum of all its digits, e.g. 
the digit sum of 84001 is calculated as 8+4+0+0+1 = 13,
the digit sum of 158 is 1+5+8 = 14.
Rohan's teacher has asked him to write a function (method) that takes as input a positive number and performs digitSum of either only the even digits or only the odd digits in the given number, based on the option "even" or "odd".

The function will take two input parameters -
- the first parameter will be an integer number representing the number whose digitSum needs to be found
- the second parameter will be a string representing the option, which will be either "even" or "odd" 
Example 1: If the given number is 9625, and the option is "odd", we must add only the odd digits, i.e. 9+5 = 14
Example 2: If the given number is 2134, and the option is "even", we must add only the even digits, i.e. 2+4 = 6
Assumptions:
- The input number (input1) will be a positive integer number >= 1 and <= 25000.
- The input string (input2) will always be either "even" or "odd".



//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
public int OddDigitsSum(int input1,String input2){
      int sum=0;
        if(input2.equals("even"))
        {
while(input1!=0)
    {
 int n=input1%10;
 if(n%2==0)
    sum+=n;
  input1/=10; 
    }
        }
        else
        {
    while(input1!=0)
    {
     int n=input1%10;
   if(n%2!=0)
    sum+=n;
  input1/=10; 
    }
        }
       return sum;
    }
}

Post a Comment

1 Comments

Please do not Enter any spam link in the comment box