Bloomberg LP Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

{i=0;j=n-1;
while(i<=j)
{
if(a[i]==2&&a[j]==1)
{swap(a[i],a[j]);i++;j--;}
else if(a[i]==1) i++;
else if(a[j]==2) j--;
}
}

- MaveRick12 February 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

For C++ fans:

#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

bool isOne (int i) { return i==1; }

int main(int argc, char**argv)
{
    vector<int> a = {1,2,1,2,1,2,1,2};
    partition(a.begin(), a.end(), isOne);
    for (int i: a) 
        cout << i << endl;
}

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

crisp

- northernlight May 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very nice, could have used a C++11 lambda to make it a little cleaner though ;)

#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int main(int argc, char**argv)
{
    vector<int> a = {1,2,1,2,1,2,1,2};
    partition(a.begin(), a.end(), [](int x){ return x == 1; });
    for (int i: a) 
        cout << i << endl;
}

- Quinn December 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

can somebody explain the question little bit...why do we need to sort it

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

1. Use Count sort, if you can don't have memory restraint, also there could be {3,4,5.. } players also.
or
2. We can do in-place by setting two pointers and O(n), for these two players we can sort.

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

public void sortPlayer(int[] a)
{
int oneCount=0;
int twoCount=0;
for(int i=0;i<a.lenght();i++)
{
if(a[i]==1) oneCount++;
else twoCount++;
}
int i=0
while(oneCount !=0)
{
a[i++]=1;
oneCount--;
}
while(twoCount !=0)
{
a[i++]=2;
twoCount--;
}
}

- Danish Shaikh February 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[] playerOrganize(int[] arr)
{
	int left =0; 
	int right = arr.length-1;
	int [] newArr = new int[arr.length];
	for (int i =0; i< arr.length; i++)
	{
		if (arr[i] == 1)
		{
			newArr[left] = 1;
			left++;
		}
		else
		{
			newArr[rigth] = 2;
			right--;
		}
	}
	return newArr;
}

public int[] playerOrganize(int[] arr)
{
	int left =0; 
	int right = arr.length-1;
	for (int i =0; i< arr.length/2; i++)
	{
		while (arr[i]!=1)
		{
			int temp = arr[i];
			arr[i] = arr[right];
			arr[right] = temp;
			right --;
		}
		
	}
	return arr;
}

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

Another method for in-place sorting is "Partition". This works since there is only 2 players.

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

public class player {
	public static void main(String args[]){
		int[] player = {1,2,1,2,1,2,1,2};int i=0;
		ArrayList<Integer> player1 = new ArrayList<Integer>();
		ArrayList<Integer> player2 = new ArrayList<Integer>();
		for( i =0;i<player.length;i++){
			if(player[i] == 1)
				player1.add(1);
			if(player[i] == 2)
				player2.add(2);
		}
		for( i=0;i<player1.size();i++)
			player[i] = player1.get(i);
		for(int j=0;j<player2.size();j++){
			player[i] = player2.get(j);
			i++;
		}
		for( i =0;i<player.length;i++)
		System.out.println(player[i]);
	}
}

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

None of the above solution take care of more than two players. The player array could be {1,2,3,4,1,2,3,4,1,2,3,4}

Also, sorting doesn't work. In this question, players are represented by consecutive numbers, so it seems that sorting will work. But the notion is that player can be represented by key... So, instead of the above array, the player array could be [10,1,100, 10,1,100, 10,1,100].. Sorting will fail here..

This code will take care of both the cases.

int[] players = { 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4 };
    
    int len = players.length;
    
    List<Integer> list = new ArrayList<Integer>();
    int index = 0;
    while ( true ) {
      list.add( players[index] );
      index++; 
      if (players[index] == list.get(0)) {
        break;
      }
    }
    
    int repeat = len/list.size();
    for ( int i = 0 ; i < list.size(); ++i ) {
      for ( int j = 0; j < repeat; ++j ) {
        players[i*repeat+j] = list.get(i);
      }
    }

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

Why will sorting fail on [10,1,100, 10,1,100, 10,1,100]? This is just an int array and can be sorted like any other int array.

- Johannes April 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

apply counting sort. count element 1, and 2. Rearrange new array

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

partition(a.begin(), a.end(), [](int i) -> bool{
    	return i == 1;
    });

- Eric November 04, 2014 | 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
	{
		int[] arr = {1,2,1,2,1,2,1,2,1,2};
        int[] result = sort(arr);
		for(int i=0; i<10; i++)
			System.out.println(result[i]);
	}
	
	public static int[] sort(int[] arr)
	{
		int len = arr.length, head=0, tail = len-1;
		int[] result = new int[len];
		for(int i=0; i<len; i++)
		{
			if(arr[i]==1)
				result[head++] = 1;
			else
				result[tail--] = 2;
		}
		
		return result;	
	}
	
}

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

public static void main(String[] args) {
		int x [] = {1,2,1,2,1,2};
	
		
		
		int y=0;
		for (int i=0; i<x.length; i++)
		{
			               
			for (int j=i; j>0 && x[j-1]>x[j]; j--)
			{
				
			                 
			                	  y = x[j-1];
			                          x[j-1] = x[j];
			                          x[j] = y;
			   
			} 	 
		}	                	 
			                	 
		for (int i=0;i< x.length;i++)
		{
			System.out.println(x[i]);
		}
		


	}

- mandar March 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorting seems the right answer. Is it not?

- mike800 June 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static void doTheThing2()
    {
        int arr[] = { 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 };
        int N = arr.length;
        for (int i = 1; i < N / 2; i += 2)
        {
            arr[i] = 1;
        }
        for (int j = N - 2; j >= N / 2; j -= 2)
        {
            arr[j] = 2;
        }
        System.out.println(Arrays.toString(arr));
    }

- rks February 11, 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