Interview Question


Country: United States
Interview Type: Phone Interview




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

C# Solution

/* Problem: Print Fibonacci numbers between (n,m)
         * 
         * Assumptions: m > n
         * 
         * Algorithm:
         * 1. Start calculating Fibonacci numbers from 0
         * 2. If the number lies between desired range, print it.
         * 
         * Complexity: O(N)
         * 
         * Test cases:
         * - (0, 1)   => outputs 0,1,1. This falls between (0, 32) 
         * - (0, 32)
         * - (5, 32)
         * 
         * - (-1, 3) => fail
         * - (32, 0) => fail
         */
        public static void FindFibonacciNumbersInRange(int n, int m)
        {
            if (n < 0 || m < 0 || n >= m)
                throw new ArgumentException("enter correct range");

            int a = 0;
            int b = 1;

            //Print out the first two numbers if in range.
            if (n == 0)
            {
                Console.WriteLine(a);
                Console.WriteLine(b);
            }
            if (n == 1)
                Console.WriteLine(b);

            int c = a + b;
            

            while (c <= m)
            {
                if (c >= n)
                    Console.WriteLine(c);
                a = b;
                b = c;
                c = a + b;
            }
        }

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

a simplest code will look like this:
int f,f1,f2;
f1=0,f2=1;
for(i=0;i<7;i++)
{
f=f1+f2;
f1=f2;
f2=f
printf("%d",f);
}

- unknown February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not print the first two fibonacci numbers.
Fibo numbers are 0,1,1,2,3,5...

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

sorry,i think it would work..

int f,f1,f2;
f1=0,f2=1;
printf("%d%d",f1,f2);
for(i=0;i<7;i++)
{
f=f1+f2;
f1=f2;
f2=f
printf("%d",f);
}

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

public void fibo(int low, int high){


int[] arr = new int[];
arr[0] = 0;
arr[1] = 1;

if(arr[0] >= low && arr[0] <= high){
System.out.print(arr[0] + " ");
}

if(arr[1] >= low && arr[1] <= high){
System.out.print(arr[1] + " ");
}

int i = 2;
while(arr[i] <= high){

arr[i] = arr[i-1] + arr[i-2];
if(arr[i] >= low && arr[i] <= high){
System.out.print(arr[i] + " ");
}
i++;
}

}

- jessica.ece2012 February 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

find("Fibonacci numbers between 0-32");

- Anonymous February 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int f,f1,f2;
f1=0,f2=1;
printf("%d%d",f1,f2);
for(i=0;i<32;i++)
{
f=f1+f2;
f1=f2;
f2=f
printf("%d",f);
}

- suman February 09, 2013 | Flag


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