Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

void fib(int start, int length)
{
  if (start<0)
  {
    cout<<"invalid start"<<endl;
    return;
  }
  else if(length<=0)
  {
    cout<<"invalid length"<<endl;
    return;
  }
  if ( (start==0) && (length>0) )
  {
    cout<<"0 ";
    length--;
    if(!length)
    {
      cout<<endl;
      return;
    }
  }
  int a = 0;
  int b = 1;
  int current = 1;
  while(current<start)
  {
    int c = a + b;
    a = b;
    b = c;
    current++;
  }
  while(length)
  {
    int c = a + b;
    a = b;
    b = c;
    cout<<c<<" ";
    length--;
  }
  cout<<endl;
}

- Rayden January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

u may also have to check "int start" 's overflow condition

- shivacherukuri January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

void fibonacci(int n, int len);
{
if(len<=0 ||n<0)cout<<"wrong Input";
else
{
for(int i=1; i<=len;i++)
cout<<fibo(n-1,i);
}
}
int fibo(int n, int len)
{
if(len==1)return n;
if(len==2)return (n+1);
return fibo(n-2) +fibo(n-1);
}
//test case:
1.(0,0)
2.(0,1)
3.(0,2)
4.(5,0)
5.(5,5)
6.(-1,-1)

- Ali_BABA January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Fibonacci
    {
        static Dictionary<int, int> fibMap = new Dictionary<int, int>();
        public static int RunFibonacci(int startIndex, int len)
        {
            int fib = 0;
            if (fibMap.ContainsKey(len))
            {
                return fibMap[len];
            }

            if (len == 1 || len == 2)
            {
                fib = 1;
            }
            else
            {
                fib = RunFibonacci(startIndex, len - 2) + RunFibonacci(startIndex, len - 1);
            }

            fibMap.Add(len, fib);
            if (len >= startIndex)
            {
                Console.WriteLine(fib);
            }

            return fib;
        }

        public static void FibonacciTest()
        {
            RunFibonacci(1, 10);
            Console.WriteLine("===");
            fibMap.Clear();
            RunFibonacci(2, 6);
            Console.WriteLine("===");
            fibMap.Clear();
            RunFibonacci(3, 5);
            Console.WriteLine("===");
        }
    }

- InterviewSkills January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <cstdlib>

int main()
{
	int lower = 0;	// fibo series: 0, 1, 1, 2, 3, 5 ...
	int upper = 1;
	int temp = 0;

	// ------------------- user input goes here ----------------------------- //
	int startPoint = 1; // from 1st element, assuming series starts from 0th element
	int range = 4;		// 5 elements including startPoint
	// ---------------------------------------------------------------------- //

	if(startPoint>=0 && range > 0)
	{

		int endPoint = startPoint + range - 1;	
		
		if(startPoint < 2)
		{	
			while(startPoint != 2 && range != 0)	// this loop executes at most 2 times
			{
				std::cout<< startPoint++ <<" ";		
				range--;
			}
			endPoint = startPoint + range - 1;		//recalculate
		}
			
		while(endPoint>=2)		// iterate
		{
			temp = lower;
			lower = upper;
			upper = lower + temp;
			endPoint--;	
			if(endPoint <= range)
				std::cout << upper << " ";
		}
	}
	else
	{
		std::cout<<"invalid input!";
	}

	getchar();
	return 0;

}

- maverick January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

kl

- 789654123 January 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] fib(int st, int len)
{
int[] a = new int[len];

if (st == 0 && len == 1)
{
a[0] = 0;

} if (st == 1 && len == 1)
{
a[0] = 1;
}

if (len > 2)
{
a[0] =0;
a[1] =1;
for (int i = 2; i <= len + st; i++)
{
a[(i) % len] = a[(i - 1) % len] + a[(i-2) % len];
}
}
return a;
}

- kk January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fibo(int n) {
if (n < 0) return -1; // Error condition.
if (n == 0) return 0;
int a = 1, b = 1;
for (int i = 3; i <= n; i++) {
int c = a + b;
a = b;
b = c;
}
return b;
}

void fib( int s, int e)
{
for(int i=s ; i <(s+e); i++)
{
cout<<fibo(i)<<endl;
}
}

- abc February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fibo(int n) {
if (n < 0) return -1; // Error condition.
if (n == 0) return 0;
int a = 1, b = 1;
for (int i = 3; i <= n; i++) {
int c = a + b;
a = b;
b = c;
}
return b;
}

void fib( int s, int e)
{
for(int i=s ; i <(s+e); i++)
{
cout<<fibo(i)<<endl;
}
}

- abc February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool fibo(int start,int length)
{
    if (start < 0)
    {
        cout << "Invalid start";
        return false;
    }

    if (length < 0)
    {
        cout << "Invalid length";
        return false;
    }

    if (length == 0)
    {
        cout << "Lenght = 0";
        return true;
    }
    int a = 0;
    int b = 1;
    int nr = 1;

    if (start == 0)
    {
        cout << a << " ";
        length--;
    }

    if (start == 1)
    {
        cout << b << " ";
        length--;
    }

    while (nr < start)
    {
        a += b;
        b = a - b;
        nr++;
    }

    while (length--)
    {
        a += b;
        b = a - b;
        cout << a <<' ';
    }
    return true;
}

- save.the.w. February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
     * Print out fibonacci numbers from a start point and for a specific length
     * @param start start index
     * @param length number of count to print out
     */
    public static void printFib(int start,int length){
        int last2=0,last=1;
        int count=0;
        int current=0;
        if(start<0 || length==0)return;
        while(count<start+length){
            if(count==0){
                current=last2;
            }else if(count==1){
                current=last;
            }else{
                current=last+last2;
                last2=last;
                last=current;
            }
            if(count>=start){
                System.out.println(""+current);
            }
            count++;
        }
    }

- Test May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void fib(int start, int len) {
 int a = 0, b = 1, tmp;
 while (len > 0 && startIdx >= 0) {
  if (startIdx == 0) printFib(a); else startIdx--;
  len--; tmp = a + b;  a = b; b = tmp;
 }
}

- GKalchev June 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int[] f = new int[100];

        internal void fibo(int start, int length)
        {
            f[0] = 0; // initialize first position to zero
            for (int i = 0; i < length; i++)
            {
                int num = fibo(start + i);
                Console.WriteLine(num);
            }
        }

        private int fibo(int n)
        {
            if (n == 0) return 0;
            if (n == 1) return 1;

            f[n - 1] = fibo(n - 1);

            return f[n - 1] + f[n - 2];

}

- sk January 18, 2012 | 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