Morgan Stanley Interview Question for Software Engineer / Developers






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

Check whether the given array follows the Fibonacci property.
eg if we are given 23,34,56,....
we know it cant be in Fibo seq as 23+34!=56

In case the given numbers follow the property
we can use the definition of Fibo seq..
F(n+1)=F(n)+F(n-1)
=> F(n-1) = F(n+1) - F(n)

in this if we go down then we should get 2,1,0 as last (+) nums.

eg for 23,34,57,....
other nums 'ld be 11,12,-1
so not in Fibo

- AP March 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is one written in Java,

public static boolean checkFibonacci (int array[])
{


for (int i=0; i < array.length/2; i++)
{

if(array[i] + array[i+1] ! = array [i+2])
{
return false;
}
}
if( array.length % 2 !=0 )
{
if (array[length -1] != array[length -2] + array[length -3])
{
return false;
}
}

return true;

}

- Abid February 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this doesnt check if the first two digits are part of a fib sequence

I would think you have to start from 1 computing till you got to the first value first and check those values if you never reach a value greater then the first value w/o reaching the first value return false else compute like above.

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

this doesnt check if the first two digits are part of a fib sequence

I would think you have to start from 1 computing till you got to the first value first and check those values if you never reach a value greater then the first value w/o reaching the first value return false else compute like above.

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

public class isFibonacci{
public static void main(String args[]){
//int [] arr = {1,1,2,3,5,8,13,21,34,55,89,144};
int [] arr = {1,1,2,3,5,8,13,21,34,55,89,144};
int sum=0;
int i=1;
boolean flag = false;
while(i<arr.length-1){
sum =arr[i]+arr[i-1];
if(sum!=arr[i+1]){
flag=false;
System.out.println("Not a fibonacci sequence.");
break;
}
i++;
flag = true;
}
if(flag) System.out.println("A valid fiboncaai sequence.");
}
}

- Abhishek March 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

N is a Fibonacci number if and only if (5*N*N + 4) or (5*N*N - 4) is a perfect square.

- chethanjs May 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct.

- Anonymous. May 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

may cause overflow..!!

- Sukhmeet2390 August 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class FibonacciChecker {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] data={1,1,2,3,5,8,13};
		System.out.println("isFib : "+isFibonacci(data));
	}
	
	public static boolean isFibonacci(int[] data)
	{
		for(int i=2;i<data.length;i++)
		{
			int sum=data[i-2]+data[i-1];
			if(sum!=data[i])
				return false;
		}
		return true;
	}

}

- Anonymous June 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class FibonacciChecker {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] data={1,4,5,9};
		System.out.println("isFibonacci : "+isFibonacci(data));
	}
	
	private static boolean isFibonacci(int[] data)
	{
		if(data.length<2)
			return false;
		if(!checkForStartingNumbers(data[0])||!checkForStartingNumbers(data[1]))
			return false;
		for(int i=2;i<data.length;i++)
		{
			int sum=data[i-2]+data[i-1];
			if(sum!=data[i])
				return false;
		}
		return true;
	}
	
	private static boolean checkForStartingNumbers(int number)
	{
		int n=5*number*number+4;
		int n1=5*number*number-4;
		int sqrt1st=(int)Math.sqrt(n);
		int sqrt2nd=(int)Math.sqrt(n1);
		if(n==sqrt1st*sqrt1st||n1==sqrt2nd*sqrt2nd)
			return true;
		return false;
	}

- Vivek Grewal June 14, 2014 | 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