Bloomberg LP Interview Question for Software Engineer / Developers


Team: Price history
Country: United States
Interview Type: In-Person




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

public static void fibonacci(int n) {
		int prevNumber = 0;
		int currentNumber = 1;
		while (currentNumber <= n) {
			System.out.println(prevNumber);
			int temp = currentNumber;
			currentNumber = currentNumber + prevNumber;
			prevNumber = temp;
		}
		System.out.println(prevNumber);
	}

- RajKP August 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int main()
 {
     int n, i;
     printf("Enter the number of elements in the sequence");
     scanf("%d", &n);
     for( i=0 ;i<n;i++)
    {
       printf("%d",fibo(i));
  }
}
int fibo(int n) {
 if ( n == 0)
  return 0;
else if (n ==1)
   return 1;
 else
   return fibo(n-1) + fibo(n-2);
}

- Arpitha August 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

All the calls to fibo from 0 to n-1 are unnecessary and make this O(N^2) instead of O(N). Instead try something like this:

int fibo(int n) {
 if ( n < 2)
  return n;
 else {
   int fibn = fibo(n-1) + fibo(n-2);
   printf("%d", fibn);
   return fibn;
 }
}

and call with

fibo(n);

- tp September 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

def FIBO(f0, f1, n):
    print f0
    while(f1<=n):
        print f1
        f1 = f0 + f1
        f0 = f1-f0

FIBO(0,1,89)

- Aerofoil.kite December 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Solution:

public static void main(String[] args) {

		System.out.println("Enter the no of elements");
		Scanner sc1 = new Scanner(System.in);
		int n = sc1.nextInt();
		
		System.out.println(displayFibonacci(n).toString());
		
	}
	
	private static StringBuffer displayFibonacci(int n){
		
		StringBuffer buffer = new StringBuffer();
		List<Integer> list = new ArrayList<Integer>();
		
		for(int i=0; i < n; i++){
			if(i == 0){
				list.add(0);
				buffer.append(String.valueOf(0));
			}
			
			if (i == 1 || i == 2){
				
				list.add(1);
				buffer.append(",");
				buffer.append(String.valueOf(1));
			}
			
			if(i > 2){
				list.add(list.get(i-1) + list.get(i-2));
				buffer.append(",");
				buffer.append(list.get(i-1) + list.get(i-2));
			}
		}
		
		return buffer;
	}

- Amit K Bist September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Iterative Java Solution O(N), recursive one will take exponential time O(2^n)

public int fibIter(int n){
	  
	  int fn=0, fnMinus1=1, fnMinus2=0;
	 
	  for(int i=2; i<=n; i++){

		  fn=fnMinus1+fnMinus2;
		  fnMinus2=fnMinus1;
		  fnMinus1=fn;
		  System.out.print(fn);
	  }
	  System.out.print(fn);
	  return fn;
  }

- ATuring September 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Scanner;

public class Fibonacci {

public void fib(int size, int firstNum) {
int previous = 0;
int current = firstNum;
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(current);
for(int i = 1 ; i<size ; i++) {
int temp = current;
current = previous + temp;
previous = temp ;
list.add(current);
}
System.out.println("The fibonnaci series is :");
System.out.println(list);
}

public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first element");
int firstNum = scanner.nextInt();

System.out.println("Enter the size of the Fibonacci Series");

int size = scanner.nextInt();
Fibonacci object = new Fibonacci();
object.fib(size,firstNum);
}

}

- S.O. November 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dynamic Programming Approach:

Avoid the repeated work done (By recursion method) by storing the Fibonacci numbers calculated so far.
This can even further optimized the space by storing the previous 2 numbers only because that is all we need to get the next Fibonnaci number in series.

int fibo ( int Num ) {
  	int prev1 = 0, prev2 = 1, next ;

  	if( 0 == Num )
    		return prev1 ;

  	for (int i = 2; i <= num;  i++ )  {
     		next = prev1 + prev2 ;
     		prev1 = prev2 ;
     		prev2 = next ;
  	}
  	return prev2;
}

Time Complexity: O(N)
Space Complexity: O(1)

- R@M3$H.N January 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		fib(10);
	}
	
	public static void fib(int n)
	{
			
		int p=1,q=1;
		while (p<n)
		{
			System.out.println(p);
			int temp;
			temp = q;
			q = p + q;
			p = temp;
		}
			 
	}

}

- naomi.lijing@googlemail.com February 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Dynamic Programming Would be the best approach

- Anonymous September 06, 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