Microsoft Interview Question






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

bool IsPrime(int N){
if(<=0)return false;
if(N<=3)return true;

for(int i=2;i<N;i++) {
if( (N/i)*i = N)return true;
}
return false;
}

- test April 23, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsPrime(int N)
{
if(N<0 || N%2==0)
return false;
for(int i =3;i<N;i=i+2)
{
if((N/i)*i==N)
return true;
}
return false;
}

}

- Anonymous May 24, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

>> for(int i =3;i<N;i=i+2)

You need not check till N it is ok to check till sqrt(N)

- Nagesh Ayyagari September 11, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Did not get. "if((N/i)*i==N)" isn't this always true?

- Ramu September 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it not N%i==0 ?

- Vamsi December 05, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPrime( long num )
{
  if (num < 2) return false;
  if (num < 4) return true;
  if (num % 2 == 0) return false;
  long i, x = num;
  int count = 0;
  for (i = 3; i < x; i+=2)
  {
    if((num % i) == 0) return false;
    if (count++ == 100)
    {
      count = 0;
      x = num/i; // we can do this in every iteration, but to speed up let us do it every 100th iteration.
    }
  }
  return true;
}

- Raaj February 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<math.h>
int prime(int n);

int main(){
int n;
cout << "Enter N:";
cin >> n;

cout << "Prime:" << prime(n) << "\n";
return 0;
}

int prime(int n){
int i = 2;
int l = int(sqrt(n));
int flag = 1;
while( i <= l ){
if ( n%i == 0 ){
flag = 0;
break;
}
++i;
}
return flag;
}

- cnb December 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My code :

unsigned char is_num_prime(int num)
{
    if(num == 1)
        return 0;
    else if(num >= 2 && num <= 3)
        return 1;
    else if(num % 2 == 0)
        return 0;
    else
    {
        int i = 3;
        float fact = ((float)num)/i;
        while(fact > 1.0f)
        {
            if(num % i == 0)
                return 0;
            i += 2;
            fact = ((float)num)/i;
        }
    }
    
    return 1;
}

- Srikant Aggarwal December 21, 2011 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More