Cracking the coding interview | Step-3

DAY 3:coding interview questions


Question 1:

isPrime?

Write a function that finds whether the given number N is Prime or not.
If the number is prime, the function should return 2 else it must return 1.
Assumption: 2 <= N <=5000, where N is the given number.
Example1: if the given number N is 7, the method must return 2
Example2: if the given number N is 10, the method must return 1

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int isPrime(int input1){
  int count=0;
 for(int i=2;i<=Math.sqrt(input1);i++)
{
 if(input1%i==0)
     count++;
}
if(count==0)
 return 2;
 else
return 1;
 }
}

Question 2:

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!= 5 X 4 X 3 X 2 X 1 = 120
5!= 4 X 3 X 2 X 1 = 24
9!= 9 X 8 X 7 X 6 X 5 X 4 X 3 X 2 X 1 = 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.
The assumption 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 number will range from 1 to 12.

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int nFactorial(int input1){
  int fact=1;
for(int i=1;i<=input1;i++)
 {
fact=fact*i;
 }
return fact;
 }
}

Question 3:

nth Fibonacci: Write a function to return the nth number in the Fibonacci series.

The value of N will be passed to the function as an input parameter.
Note: Fibonacci series looks like -
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...... and so on.
i.e Fibonacci series starts with 0 and 1 and continues generating the next numbers as the sum of the previous two numbers.
first Fibonacci number is 0,
second Fibonacci number is 1,
third Fibonacci number is 1,
fourth Fibonacci number is 2,
fifth Fibonacci number is 3,
sixth Fibonacci number is 5,
seventh Fibonacci number is 8, and so on.


//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
 public long nthFibonacci(int input1){
 if(input1==1)
 return 0;
if(input1==2)
 return 1;
 else
 return nthFibonacci(input1-1)+nthFibonacci(input1-2);
 }
}

Question 4:

Nth Prime

Write a function that finds and returns the Nth prime number. N will be passed as input to the function.
Assumption: 1 <= N <=1000, where N is the position of the prime number
The first prime number is 2
The second prime number is 3
The third prime number is 5
The fourth prime number is 7
the fifth prime number is 11
... and so on.
Example 1: If the given number N is 10, the method must return the 10th prime number i.e. 29
Example 2: If the given number N is 13, the method must return the 10th prime number i.e. 41

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int NthPrime(int input1){
       int count=0,pcount=0,i;
  for(i=2;i<=100000;i++)
  {
   count=0;
   for(int j=2;j<=Math.sqrt(i);j++)
   {
    if(i%j==0)
     count++;
   }
   if(count==0)
    pcount++;
   if(pcount==input1)
    break;
  }
  return i; 

}}

Cracking the coding interview | Step-2       Cracking the coding interview | Step-4

Post a Comment

0 Comments