Bloomberg LP Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
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;
}
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;
}
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;
}
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]);
}
}
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);
}
}
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;
}
}
{i=0;j=n-1;
- MaveRick12 February 11, 2014while(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--;
}
}