Goldman Sachs Interview Question for Developer Program Engineers


Country: India
Interview Type: In-Person




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

But as you probably know, this is example of extremely inefficient implementation, in recursive calls,lot of things is calculated many times...

The better one is based on storing already calculated values..

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

|a[n+1] a[n] | | 1 1 | power n
|a[n] a[n-1] |= | 1 0 |
These are 2D matrices. This is the best possible way to find fibonacci numbers.

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

If it wants to print all n fab number, the iterative solution should be most efficient one.

- chenlc626 March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	int i;
	int NUM = atoi(argv[1]);
	int t1=1;
	int t2=1;
	int S;
	printf("%d %d ", t1, t2);
	for(i=3;i<=NUM;i++){
		S = t1 + t2;
		t1 = t2;
		t2 = S;
		printf("%d ", S);
	}
	printf("\n");
	return 0;
}

- SAM_THE_EXPERT May 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- SAM_THE_EXPERT May 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
 
void multiply(int F[2][2], int M[2][2]);
 
void power(int F[2][2], int n);
 
/* function that returns nth Fibonacci number */
int fib(int n)
{
  int F[2][2] = {{1,1},{1,0}};
  if(n == 0)
    return 0;
  power(F, n-1);
  return F[0][0];
}
 
/* Optimized version of power() in method 4 */
void power(int F[2][2], int n)
{
  if( n == 0 || n == 1)
      return;
  int M[2][2] = {{1,1},{1,0}};
 
  power(F, n/2);
  multiply(F, F);
 
  if( n%2 != 0 )
     multiply(F, M);
}
 
void multiply(int F[2][2], int M[2][2])
{
  int x =  F[0][0]*M[0][0] + F[0][1]*M[1][0];
  int y =  F[0][0]*M[0][1] + F[0][1]*M[1][1];
  int z =  F[1][0]*M[0][0] + F[1][1]*M[1][0];
  int w =  F[1][0]*M[0][1] + F[1][1]*M[1][1];
 
  F[0][0] = x;
  F[0][1] = y;
  F[1][0] = z;
  F[1][1] = w;
}
 
/* Driver program to test above function */
int main()
{
  int n = 9;
  printf("%d", fib(9));
  getchar();
  return 0;
}

- Putta June 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def fibs(n):
		result = [0,1]
		for i in range(n-2):
			result.append( fibs[-2] + fibs[-1] ) 
		return result

- Anonymous August 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def fibs(n):
		result = [0,1]
		for i in range(n-2):
			result.append( fibs[-2] + fibs[-1] ) 
		return result

- K Abhishek Das August 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dynamic programming approach:
1. Use Map to store calculated key/value pair.
2. BEfore using standard recursive approach, check if map contains calculated number. It it contains, then just return value else call function recursively.

static Map<Integer, Integer> map = new HashMap<Integer,Integer>();
map.put( 0, 0 );
map.put( 1, 1 );
DP_Fibonacci(40);

Set<Integer> keys = map.keySet();
for ( Integer key : keys ) {
System.out.print( map.get( key ) + " " );
}


int DP_Fibonacci( int n ) {
if ( !map.containsKey( n ) ) {
map.put( n, DP( n - 1 ) + DP( n - 2 ) );
}
return map.get( n );
}

- cse September 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This seems to be the typical fizzbuzz problem. For printing first n fibonacci numbers, the simple iterative algorithm should do. A python example:

def print_fib(n):
    if n>=1:
        print 1
    if n >= 2:
        print 1
    a,b = 1,1
    n -= 2
    while n > 0:
        a,b, = b, a+b
        print b
        n -= 1

- Loler March 05, 2013 | 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