Intuit Interview Question for Software Engineer / Developers






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

Matrix m = new Matrix(new row(1,1), new row(1,0));

Matrix FibN = m.power(n);

return FibN.Row(1).Column(1)


// Assume some implementation of Matrix...
// m = [1 1]
// [1 0]
//

- Anonymous December 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

package com.badal.careercup;

import java.util.ArrayList;

public class Fibonacci {

private ArrayList<Integer> generateFibonacci (int n){
ArrayList<Integer> fib=new ArrayList<Integer>();
fib.add(0);
fib.add(1);
for(int i=2;i<n;i++){

fib.add(fib.get(i-1)+fib.get(i-2));

}
return fib;
}

public static void main(String[] args){
Fibonacci f=new Fibonacci();
System.out.println(f.generateFibonacci(13));

}

}

- badalrocks January 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I take that back, badal. I do apologize. You code is not recursive, and it does help show a dynamic programming kind of approach (but not really dynamic programming).

I am the one who should follow my own advice...

- T March 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And here is a dynamic programming kind of approach.

(In C# like code)

public static Fibonacci {
    private static Dictionary<long, long> _fibonacci;
    
    static Fibonacci() {
        _fibonacci = new Dictionary<long, long>();
        _fibonacci[0] = 0;
        _fibonacci[1] = 1;
    }
    
    // Gets the n'th fibonacci number.
    public static long Get(long n) {
        
        if (n <= 0} {
            throw new InvalidArgumentException("-ve");
        }
        
        long value;
        if (_fibonacci.TryGetvalue(n, out value)) {
            return value;
        }
        
        return Get(n-1) + Get(n-2);
    }
}

- T March 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That must be n < 0, not n <= 0.

- T March 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

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

- ednap December 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous December 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is O(log n) time algorithm.

- Anonymous December 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Fibonacci(int f)
{
int n = 1;
int nminus = 0;
for (int i = 0; i <= f - 2; i++)
{
int nhold = nminus;
nminus = n;
n = n + nhold;
}
return n;
}

Recursion is nice and all but will quickly bog down with large f numbers...

for the sequence simply change the return type and do a list...

IEnumerable<int> Fib(int f)
{
int n = 1;
int nminus = 0;
for (int i = 0; i <= f - 2; i++)
{
int nhold = nminus;
nminus = n;
n = n + nhold;
yield return n;
}
}

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

Here's Python:

def fib():
    a0,a1 = 0,1
    while 1:
        yield a0
        a0,a1 = a1,a0+a1

fibber = fib()
print(next(fibber)) # 0
print(next(fibber)) # 1
print(next(fibber)) # 1
print(next(fibber)) # 2
# and so on as many times as you like

- Bullocks December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Fabonacci
{
public static void main( String [] args)
{
int a = 0;
int b = 1;
System.out.print(" " + a +" ");
System.out.print(" " + b +" ");
for (int i=0;i <=10; i++)
{
int c = a+b;
System.out.print(" " + c +" ");
b=c;
a=b-a;
}
}
}

- Shekhar Muley September 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Fibonacci series is based on the distribution of chloroplasts. When they divide, they are of opposite charge. The same in java can be done as below. The benefit of the following code is that it doesnt require additional sysouts.

class Fibo {
	public static void main(String[] args) {
		int a = -1, b = 1, c = 0;
		for(int count=0; count < 10; count++) {
			c = a + b;
			System.out.println(c);
			a = b;
			b = c;
		}
	}
}

- Anvesh Kumar Mallick September 19, 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