Amazon Interview Question






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

void printFibonacci(int ith)
{
	std::cout << endl;
	int num1 = 0;
	int num2 = 1;
	std::cout << setw(4) << num1;
	std::cout << setw(4) << num2;
	for(int i = 3; i <= ith; i++)
	{
		int temp = num1 + num2;
		std::cout << setw(4) << temp;
		num1 = num2;
		num2 = temp;

	}
	std::cout << endl;
}

- my implementation January 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is not recursive however, this is a standard loop approach

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

void rFibonacci(int pCount)
{
        static int i = 0;
        static int j = 1;
        int temp = 0;
        if(pCount)
        {
                printf("%d ",i);
                temp = i+j;
                i = j;
                j = temp;
                pCount--;
                rFibonacci(pCount);
        }
}

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

very nice, but i screwed up since i was trying to think from top to bottom , as in
fib(n-1)+fib(n-2) approach :( and there was time constraint to write program

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

The above program may not impress the interviewer very much..it is not advisable to use static variables in recursion as it defeatss the whole puropse

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

void printFib(int pCount, int i=0,int j=1)
{
if(pCount){ cout<<i; pcount--;}
if(pCount) { printFib(pCount,j,i+j); }
}

- Roshan Mangal January 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RecursiveFibonacci {

    static void recursiveFibonacci(int count, int i, int j) 
    {
        if(count > 0) {
            System.out.print(i + " ");
            recursiveFibonacci(count-1, j, i+j);
        }   
    }   
    
    static void recursiveFibonacci(int count) // bootstrap function 
    {
        recursiveFibonacci(count, 0, 1);
    }

    public static void main(String args[]) 
    {
        recursiveFibonacci(6);
        System.out.println();
    }
}

- dsk January 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is nice code.

- Anonymous January 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one :)

- Anonymous January 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good soln :)

- manjesh.bits February 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

great solution, the only thing that may be confusing is that in books the first number of the serie is fibonacci of 0, in your case, to get the fibonacci of zero, we should call recursiveFibonacci(1)

- Damian February 07, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printFib(int pCount, int i=0,int j=1)
{
if(pCount){ cout<<i; pcount--;}
if(pCount) { printFib(pCount,j,i+j); }
}

- Roshan Mangal January 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fib(int n)
{
if(n=0||n=1){
return 1;
}
else{
return fib(n-2) + fib(n-1);
}

}

- AB January 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your solution cannot print sequence my frnd
the solution posted by @dsk is the one

- akku January 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
void Fib(int a, int b,int *cnt){
    printf("%d  ",a);
    (*cnt)--;
    if (*cnt <= 0) return;
    Fib(b, a+b, cnt);
}

main(int argc, _TCHAR* argv[])
{
	int count = 10;
	Fib(0,1, &count);
	return;
}

- xdong0214 January 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
int fibonacci( int n);
void printFib(int m);
int main() {
	printf("Fibonacci seq \n\n\n");
	printFib(6);
	printf("\n");
	return 1;

}
//print series till m
void printFib(int m) {
	if(m>0) {
		printFib(m-1);
		printf("%d ", fibonacci(m));
	}
}
//fibonoacci no at position n
int fibonacci( int n){
	if(n>0) {
		if(n>2) {
			return fibonacci(n-1)+fibonacci(n-2);
		} else {
			return n-1;
		}
	}
}

- Raj January 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hope this is right...

#include <iostream>

using namespace std;

int f(int n, bool x){
	int num;
	if (n == 1){
		num =0 ;
	}
	else if (n == 2){
		num = 1;
		if (x)
			cout<< 0 <<endl;
	}
	else{
		if (x)
			num = f(n-1, true) + f(n-2, false);
		else
			num = f(n-1, false) + f(n-2, false);
		}
	if (x){
		cout<< num<<endl;
	}
	return num;
}

int main(){
	f(10, true);

}

- anon February 05, 2011 | 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