Bloomberg LP Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

sieve of eratosthenes

- mike tyson October 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

C++11 sieve of eratosthenes

auto print_primes2 = [](int n) {
	if (n >= 2) {
		vector<bool> non_primes(n);
		auto mark_non_primes = [&](int p) {
			int non_prime = 0;
			for (int i = 2; (non_prime = i * p) <= n; ++i) {
				non_primes[non_prime-1] = true;
			}
		};
		for (int i = 1; i < n; ++i) {
			if (!non_primes[i]) {
				cout << i+1 << " ";
				mark_non_primes(i+1);
			}
		}
		cout << endl;
	}
};

- anonymous November 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Sieve of Atkin.

- Anonymous November 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is an implementation of Sieve of Eratosthenes in Python

def primesLessThan(n):
    l = [True for i in xrange(3,n+1)]
    for i in range(2, int(sqrt(n))+1):
        for j,elem in enumerate(l):
            if  (j+1) % i == 0 and elem:
                l[j] = False
    return [i+1 for i,x in enumerate(l) if x]

- zsljulius November 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If I understood the algorithm correctly here is the solution in Ruby

def findPrimes (nums)
		nums.sort!
		h = Hash[nums.map{ |x| [x, false] }]
		primes = [ ]
		curr = 0
		set = false
		num = 0
	
		while (curr < nums.length)
			num = nums[curr]

			h.each do |key, value|
				if(key%num == 0)
					value = true
					set = true
				end
			end
			
			primes << num if !set

			nums[curr, nums.length].each do |n|
				if (h[n] == false)
					curr = nums.index(n)
					break	
				end
			end
		end
		
		return primes
	end

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

Jave implementation of sieve

public void print_pr_n(int n){
		HashSet<Integer> composites = new HashSet<Integer>(n);
		if(n >= 1) System.out.print("1, ");
		for(int i = 2; i <= n; i++){
			if(composites.contains(i)) continue;
			System.out.print(i + ", ");
			if(i <= Math.sqrt(n)+1){
				for(int j = 2; i*j <= n; j++){
					composites.add(i*j);
				}
			}
		}
	}

- Javeed November 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Number 1 should not be a prime, I think.

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

sieve of eratosthenes. For 1 to 100.

boolean primes[] = new boolean[100];
		int i,j;
		int cnt=0;
		for(i=0;i<10;i++)
		{
			primes[i]=true;
		}
		
		for(i=2;i<Math.sqrt(10);i++)
		{
			if(primes[i]==true)
			{
				j=i*i;
				while(j < 100)
				{
					primes[j]=false;
					cnt++;
					j=j+i;
				}
			}
		}
		
		System.out.println("Primes are");
		for(i=2;i<10;i++)
		{
			if(primes[i]==true)
				System.out.print(i + "  ");
		}

- wolfengineer November 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool prime(int n){
    int x;
    int upper=n/2+1;
    int count=0;
    for(x=2;x<=upper;x++){
        if((n%x)==0){
            count=1;
            }
    }
    if(count){
        return false;
        }
    else {
        return true;
    }
    }

- code.cracker1991 November 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

javaeediag has a pretty good solution, building up a HashSet to check against. This is definitely a problem which can be solved with dynamic programming. However, we can modify that algorithm to shave off a tiny bit of run time and take it from polynomial time to linear by building our set first, rather than inside of a loop.

public static void printPrimes(int num) {
		HashSet<Integer> composites = new HashSet<Integer>(num);
		if (num >= 1)
			System.out.print("1");

		if (num >= 2)
			System.out.print(", 2");

		if (num >= 3)
			System.out.print(", 3, 5");

		if(num > 4){
			for (int i = 2; i < num; i++)
				if ((i % 3) == 0 || (i % 5) == 0 || (i % 7) == 0)
					composites.add(i);

			for (int i = 7; i < num; i += 2)
				if (!composites.contains(i))
					System.out.print(", " + i);
		}
	}

- John November 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please help me with the following 2 questions:

1. For the condition num >= 3, we print 3 & 5. But what if num = 4?

2. Shouldn't the first for loop contain a condition for i%2 ?
Wouldn't a number like 8 fail that test and hence not get added into the composites HashSet?

- jyotika January 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I modified your program. Of course, it could do with some fine tuning. It appears to be producing the output correctly. Please share your thoughts. Thanks!

public static void printPrimes(int num)
{

		HashSet<Integer>primes = new HashSet<Integer>(num);
		//Add the numbers that will help us with the conditions.
                primes.add(1);
		primes.add(2);
		primes.add(3);
		primes.add(5);
		primes.add(7);
		
		if(num > 4){
			
			for (int i = 2; i < num; i++)
			{		
				if ((i % 2) ==0 || (i % 3) == 0 || (i % 5) == 0 || (i % 7) == 0)
					continue;
				else
 					primes.add(i);
			}
			
			for (int i = 1; i < num; i++)
			{	
				if (primes.contains(i) && i<=num)
					System.out.print("\n" + i);
		
			}
	}
}

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

What about num = 121, 163, ..... Your code will take them as prime but they are not. The solution should be find sqrt(num) and take mode of numbers until num with all prime numbers less than sqrt(num). if they are not 0 than numbers are prime.

- Henry October 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Prime {

	public static void main(String[] args) {
		
		System.out.print("Enter a number : ");
		
		int number,counter;
		
		Scanner sc = new Scanner(System.in);
		number = sc.nextInt();
		
		System.out.print("Prime number before " + number + " are : ");
		
		for(int i=1;i<number;i++){
			counter=0;
			for(int j=1;j<=i;j++){
				if(i%j==0)
					counter++;
			}
		if(counter<=2)
			System.out.print(i + " ");
			
		}

	}

}

- Diwakar December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "MyHeader.h"
#include <vector>

std::vector<int> AllPrimeNumbersLessThan(int n)
{
std::vector<int> resultArray(n+1, 1);
for(int i=3; i<=n; i++)
{
if(resultArray[i]==1)
{
int mul_factor = 2;
while(mul_factor*i<=n)
{
resultArray[mul_factor*i] = 0;
mul_factor++;
}
}
}
return resultArray;
};


int main()
{
std::vector<int> vec = AllPrimeNumbersLessThan(50);
std::cout<<"All Prime Numbers less than 50 are: ";
for(int i=1; i<vec.size(); i++)
if(vec[i])
std::cout<<" "<<i;

return 0;
};

- Pankaj February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "MyHeader.h"
#include <vector>

std::vector<int> AllPrimeNumbersLessThan(int n)
{
std::vector<int> resultArray(n+1, 1);
for(int i=3; i<=n; i++)
{
if(resultArray[i]==1)
{
int mul_factor = 2;
while(mul_factor*i<=n)
{
resultArray[mul_factor*i] = 0;
mul_factor++;
}
}
}
return resultArray;
};


int main()
{
std::vector<int> vec = AllPrimeNumbersLessThan(50);
std::cout<<"All Prime Numbers less than 50 are: ";
for(int i=1; i<vec.size(); i++)
if(vec[i])
std::cout<<" "<<i;

return 0;
};

- Pankaj February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

///#include "MyHeader.h"
#include <vector>

std::vector<int> AllPrimeNumbersLessThan(int n)
{
std::vector<int> resultArray(n+1, 1);
for(int i=3; i<=n; i++)
{
if(resultArray[i]==1)
{
int mul_factor = 2;
while(mul_factor*i<=n)
{
resultArray[mul_factor*i] = 0;
mul_factor++;
}
}
}
return resultArray;
};


int main()
{
std::vector<int> vec = AllPrimeNumbersLessThan(50);
std::cout<<"All Prime Numbers less than 50 are: ";
for(int i=1; i<vec.size(); i++)
if(vec[i])
std::cout<<" "<<i;

return 0;
};\\\

- PP February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "MyHeader.h"
#include <vector>

std::vector<int> AllPrimeNumbersLessThan(int n)
{
	std::vector<int> resultArray(n+1, 1);
	for(int i=3; i<=n; i++)
	{
		if(resultArray[i]==1)
		{
			int mul_factor = 2;
			while(mul_factor*i<=n)
			{
				resultArray[mul_factor*i] = 0;
				mul_factor++;
			}
		}
	}
	return resultArray;
};


int main()
{
	std::vector<int> vec = AllPrimeNumbersLessThan(50);
	std::cout<<"All Prime Numbers less than 50 are: ";
	for(int i=1; i<vec.size(); i++)
		if(vec[i])
			std::cout<<" "<<i;

	return 0;
};

- Pankaj, trying to figure out how to preserve whitespace February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class sieveoferatosthenes {

	public static void main(String args[]){
		ArrayList<Integer> omit = new ArrayList<Integer>();
		for(int i = 2; i < 125;i++){
			if(!omit.contains(i)){
				System.out.println(i);
			}
			for(int j = i; j<125;j++){
				if(j%i == 0)
					omit.add(j);
			}
		}
	}
}

- karpagaganesh March 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sieve Implementation in Java:

int[] primes = new int[num-1];
    for (int i = 0; i < primes.length; ++i) {
      primes[i] = i+2;
    }
    
    for ( int i = 0 ; i < primes.length && primes[i] != -1 ; ++i ) {
      int jmp = primes[i];
      
      int index = i;
      while( (index+jmp) < primes.length ) {
        index = index+jmp;
        primes[index] = -1;
      }
    }

- shekhar2010us March 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrimesTillN {

	public static void main(String[] args){
		
		PrimesTillN ptn=new PrimesTillN();
		int input=30;
		for(int i=2; i<input; i++){
			if(ptn.IsPrime(i)){
				System.out.println(i);
			}
		}
		
	}
	
	public boolean IsPrime(int n){
		
		double sqrt=Math.sqrt(n);
		
		for(int i=2; i<=sqrt; i++)
		{
		  if (n%i==0){
			  return false;
		  }
		}
		return true;
	}
	
}

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

import java.util.ArrayList;

public class SmallerPrime {

public ArrayList<Integer> prime(int number) {
ArrayList<Integer> list = new ArrayList<Integer>();
int i;
int flag = 0;
for(i = (number - 1) ; i>1 ; i--) {
for(int j=2; j<i ; j++) {
if(i % j == 0){
flag = 1;
break;
}
}
if( flag == 0) {
list.add(i);
}
flag = 0;
}
System.out.println("The prime numbers are: "+list);
return list;
}
public static void main(String [] args) {
SmallerPrime obj = new SmallerPrime();
obj.prime(123);
}
}

- Anonymous November 05, 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