Microsoft Interview Question for Software Engineer in Tests






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

void PrintPrime(int n){
int[n+1] a;
a[0]=a[1]=0;
for(int i=2;i<n+1;i++)
a[i]=1;

for(i=2;i*i<n+1;i++)
if(a[i])
for(int j=i+1;j<n+1;j++)
if(a[j])
if(a[j]%a[i]==0)
a[j]=0;

for(i=0;i<n+1;i++)
{
if(a[i]) printf....
}

}

- Anonymous October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

fuck yea!

- Anonymous October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ primenumbers(int n) { primelist = (); i = 2; while (i < n) { bool = true; for each prime in primelist { if (i%prime == 0) { bool = false; break; } } if (bool == true) primelist.add(prime); } primelist.print(); } - Thiyaneshwaran S October 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

primenumbers(int n) {
  primelist = ();
  i = 2;
  while (i < n) {
    bool = true;
    for each prime in primelist {
      if (i%prime == 0) {
        bool = false;
        break;
      }
    }
    if (bool == true) primelist.add(i);
  }
  primelist.print();
}

- Thiyaneshwaran S October 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I assume 500 is max number till which we find prime number. this can be modified for more flexibility.

#include <stdio.h>

int main (int argc, char *argv[])
{
    int n ;
    int i,j,res[500];

    n = atoi(argv[1]);

    for (i=0;i<=n;i++)
        res[i] = -1;


    for (i=2;i<=n;i++)
    {
        if (res[i] == -1)
        {
                res[i] = 1 ;
                for (j=2;(i*j)<=n;j++)
                res[i*j] = 0;
        }
    }


    for (i=2;i<=n;i++)
        if (res[i] == 1)
            printf ("%d ",i);

            return 0;
}

- Anonymous November 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what should be the test cases?

- anon November 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) No need to check even numbers
2) 1 is not a prime
A C# program:

class Program
    {
        private static ArrayList primeList = new ArrayList();
        static void Main(string[] args)
        {
            getPrime(1);    // case 1: 1
            getPrime(2);    // case 2: 2
            getPrime(4);    // case 3: 4
            getPrime(197);  // case 4: a small prime itself
            getPrime(9999); ./ case 5: a middle nun-prime
            getPrime(65352); // a big one
            Console.Read();

        }

        static void getPrime(int n)
        {
            if (n < 2)
            {
                Console.WriteLine(" Number given {0} should greate than 1.", n);
                return;
            }

            primeList.Add(2);
            if (n >= 3)
            {
                primeList.Add(3);
            }
            int p = 3;
            do
            {
                p += 2;
                if (p > n) break;
                bool isPrime = true;
                for (int i = 1; i < primeList.Count; i++)
                {
                    if (p % (int)primeList[i] == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime) primeList.Add(p);

            } while (p < n);
            Console.WriteLine(" Prime numbers less than {0}:", n);
            for (int i = 0; i < primeList.Count; i++)
            {
                Console.Write("{0} ,", (int)primeList[i]);
                if (i % 10 == 9)
                    Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine("---------------------------------");

        }

    }

- kulang November 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very good logic. Appreciate it.

- Anonymous February 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

en.wikipedia.org/wiki/Sieve_of_Eratosthenes

- Nithish April 19, 2010 | 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