Cracking the coding interview | Step-2

DAY 2:coding interview questions


Question 1:


Is N an exact multiple of M?

Write a function that accepts two parameters and finds whether the first parameter is an
exact multiple of the second parameter. If the first parameter is an exact multiple of the
second parameter, the function should return 2 else it should return 1. If either of the
parameters is zero, the function should return 3.

Assumption: Within the scope of this question, assume that -

- the first parameter can be positive, negative or zero
- the second parameter will always be >=0

//Code:


import java.io.*;
import  java.util.*;
class UserMainCode{
public int isMultiple(int input1,int input2){ 

if(input1==0 || input2==0) 

   return 3; 

else if(input1%input2==0)   

   return 2; 

else 

   return 1;

}}

Question 2:


Of the given 5 numbers, How many are even?

Write a function that accepts 5 input parameters and returns the count of how many those 5 
are even.
For example,
If the five input parameters are 12, 17, 19, 14, and 115, there are two even numbers 12  and 
14. So, the function must return 2.

Similarly,
If the five input parameters are 15, 0, -12, 19, and 28, there are three even numbers 0, -12, 
and 28. So, the function must return 3.Observe that zero is also considered an even number.

//Code:


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

public int countEvens(int input1,int input2,int input3,int input4,int input5){

 int count=0;  
if(input1%2==0)  
 count++;
 if(input2%2==0)   
    count++;  
if(input3%2==0)  
   count++;  
if(input4%2==0)   
   count++;  
if(input5%2==0)   
  count++;  

return count; 

}}

Question 3:


Of the given 5 numbers, How many are odd?

Write a function that accepts 5 input parameters and returns the count of how many those 
5 are odd.
For example,
If the five input parameters are 12, 17, 19, 14, and 115, there are three odd numbers 17 and 
19 and 115. So, the function must return 3.
Similarly,
If the five input parameters are 15, 0, -12, 19, and 28, there are two odd numbers 15 and 19.
So, the function must return 2.Observe that zero is also considered an even number

//Code:


import java.io.*;

import  java.util.*;

class UserMainCode{ 

public int countEvens(int input1,int input2,int input3,int input4,int input5){  

  int count=0;  
    if(input1%2!=0)   
       count++;  

     if(input2%2!=0)   
       count++;  
     
     if(input3%2!=0)   
        count++;
  
     if(input4%2!=0)   
       count++;  
     if(input5%2!=0)   
       count++;  
    
     return count; 

    }}


Question 4:


Of the given 5 numbers, How many are even or odd?

Write a function that accepts 6 input parameters. The first 5 input parameters are of type int.
The sixth input parameter is of type string. If the sixth parameter contains the value "even", 
the function is supposed to return the count of how many of the first five input parameters are
even. If the sixth parameter contains the value "odd", the function is supposed to return the
count of how many of the first five input parameters are odd.
For example,
If the five input parameters are 12, 17, 19, 14, and 115, and the sixth parameter is "odd",
the function must return 3 because there are three odd numbers 17, 19, and 115.

If the five input parameters are 12, 17, 19, 14, and 115, and the sixth parameter is "even", the
the function must return 2 because there are two even numbers 12 and 14.

Note that zero is considered an even number.

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int countEvensOdds(int input1,int input2,int input3,int input4,int input5,String input6){
  int count=0;
  if(input1%2==0)
   count++;
  if(input2%2==0)
   count++;
  if(input3%2==0)
   count++;
  if(input4%2==0)
   count++;
  if(input5%2==0)
   count++;
  if(input6.equals("even"))
   return count;
  else
   return 5-count;
 }

}

Cracking the coding interview | Step-1   Cracking the coding interview | Step-3

Post a Comment

1 Comments

  1. How to Attempt?

    FACTORIAL of a number

    In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

    5!= 5x4 x3 x2 x1 = 120

    4! = 4x3x2x1 = 24

    91=9x8x7x6x5x4 x3 x2 x1 = 362880

    Write a program to find the factorial of a given number.

    The given number will be passed to the function as an input parameter of type int The function is expected to calculate the factorial of the given number and return it as an int type.

    JAVAZ

    1 import jav

    2 import ja

    3

    4 // Read on

    5 class User

    {

    6

    7

    8

    9

    10

    11

    12

    }

    13

    Assumptions for this program:

    The given input number will always be greater than or equal to 1 Due to the range supported by int, the input numbers will range from 1 to 12
    In java

    ReplyDelete

Please do not Enter any spam link in the comment box