EMC Interview Question for Software Engineer in Tests

Team: RSA
Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
2
of 2 vote

``````static int getNumberOfPrime(int N) {
int count = 0;
for (int i=2; i<=N; i++) {
int max = (int)Math.sqrt(i);
boolean prime = true;
for (int j=2; j<=max; j++) {
if (i%j == 0 && i != j) {
prime = false;
break;
}
}
if (prime) {
count++;
System.out.print(i + ",");
}
}
return count;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

int fun(int N)
{
int array[N] ={0};
int i= 2,j;
while(i<sqrt(N))
{
j =i;
j=j+j;
while(j<N)
{
array[j] = -1;
j = j+i;
}
i++;
}
j=0;
for(i=0;i<N;i++)
if(array[i]==-1)
j++;
return(j);
}

Comment hidden because of low score. Click to expand.
0

Can you please explain why you need array? The way I see it solved is loop till SQRT(N) if loop counter is prime increment j and return j.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````bool isPrime(int n) {
if(n == 2 || n == 3)
return true;

for(int i = 2; i <= n/2; i++) {
if(n%i == 0) {
return false;
}
}
return true;
}

int getNumberofPrime(int n) {
int count = 0;
for (int i=2; i <= n; i++) {
if(isPrime(i))
count++;
}
return count;
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.