Cracking the coding interview | Step-4

DAY 4: coding interview questions

Question 1:


Number of Prime numbers in specified range.

Write a function to find the count of the number of prime numbers in a specified range. The starting and ending number of the range will be provided as input parameters to the function.
Assumption : 2<= starting number of the range <= ending number of the range <= 7919
Example 1: If the starting and ending number of the range is given as 2 and 20, the method must return 8, because there are 8 prime numbers in the specified range from 2 to 20, namely( 2, 3, 5, 7, 11, 13, 17,19 ).
Example 2: If the starting and ending number of the range is given as 700 and 725, the method must return 3, because there are 3 prime numbers in the specified range from 700 to 725, namely( 701, 709, 719 ).

//Code :

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

Question 2:


All Digits Count

Write a function to find the count of ALL digits in a given number N. The number Will be Passed
to the function as an input parameter of type int.
Assumption: The input number will be a positive integer number>=1 and <=250000.
For e.g.
If the given number is 292, the function should return 3 because there are 3 digits in this number
If the given number is 1015, the function should return 4 because there are 4 digits in this number


//Code :

import java.io*;
import java.util.*;
class UserMainCode
{
   public int allDigitsCount(int input1)
{
   String str=Integer.toString(input1);
   return str.length();
  }
}



Question 3:


Unique Digit Count

Write a function to find the count of unique digits in a given number N. The number will be passed to the function as the input parameter of type int.
Assumption: The input number will be a positive integer number>=1 and <= 25000.
For e.g.
If the given number is 292, the function should return 2 because there are only 2 unique digits "2" and "9" int this number.
If the given number is 1015, the function should return 3 because there are only 3 unique digits "1", "0" and "5" in this number.


//Code :

import java.io.*;
import  java.util.*;

class UserMainCode{

public int uniqueDigitsCount(int input1){

String str = Integer.toString(input1);

int len=str.length();   

int count=0;

for(int i=0;i<len-1;i++){           

for(int j=i+1;j<len;j++){               

if(str.charAt(i)==str.charAt(j)) {           

count++;                     
break;                 
}} 
 }       
  return len-count;   
}}



Question 4:


Non-Repeated Digits Count

Write a function to find the count to non-reapeated digits in a given number N. The number will be passed to the function as an input parameter of type int.
Assumption: The input number will be a positive integer number>= 1 and <=25000.
Some examples are as below-
If the given number is 292, the function should return 1 because there is only 1 non-repeated digit '9' in this number
If the given number is 1015, the function should return 2 because there are 2 non-repeated digits in this number, '0' and '5'. 
If the given number is 108, the function should return 3 because there are 3 non-repeated digits in this number, '1', '0', and '8'. 
 If the given number is 22, the function should return 0 because there are NO non-repeated digits in this number. 


//Code :

import java.io.*;
import  java.util.*;
class UserMainCode
{
public int nonRepeatDigitsCount(int input1){
String str = Integer.toString(input1);
int len=str.length();
int count=0,pcount=0;
for(int i=0;i<len;i++)
{
          count=0;
for(int j=0;j<len;j++)
{
                if(i!=j)
if(str.charAt(i)==str.charAt(j))
{
count++;
break;
}
}
            if(count==0)
              pcount++;
}
return pcount;
}
}

Post a Comment

1 Comments

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

    int count=0;
    int pcount=0;
    for(int i=input1;i<=input2;i++){

    count=0;
    for(int j=2;j<=Math.sqrt(i);j++) {

    if(i%j==0)
    count++;
    }
    if(count==0)
    pcount++;
    }
    return pcount;
    }}

    ReplyDelete

Please do not Enter any spam link in the comment box