Bloomberg LP Interview Question for Financial Software Developers






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

int array1[20] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; 
int array2[20] = { 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40};

//array1 will contain even. array2 will contain odd.
i=  0; j = 0;
while( i < 20 && j < 20 )
{
    while(i<20 && array[i]%2 == 0)
        i++;
    while(j<20 && array[j]%2 == 1)
        j++;
    //swap only when i < 20 && j < 20 cause there is a possibilty that one could be over before the other.
    if( i<20 && j<20)
    {
        //swap contents of i and j
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
        i++; j++;
    }
}

- Girish January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we do not need to scan two arrays. The number of even and odd are same:2/n. while(i<n || j<n) will be enough.

- xiao January 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i did not mention the array is sorted. so scanning the whole array is necessary

- aa October 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this should b fine.

- Rocky January 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what an uninteresting problem. i hope i get questions like this.

- Anonymous January 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution will fail if negative odd numbers appear in either arrays. The reason: (-231 % 2) does not equals to 1!

- john1732 January 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this will work on negative numbers.
as (-231 % 2 ) equals to 1

- Anonymous February 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

john1732, test before you say anything

- Anonymous October 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In Java, (-231%2) equals to -1. It might be language dependent.

- Anonymous March 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{complexity for the solution is O(n^2)}

- Anonymous January 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's O(n), because the elements in both arrays were scanned only once.

- Victor September 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

#define ODD 1
#define EVEN 0

int main ()
{

int n = 6, i, j, rep = 0, niter = 0, swap;

int ar1[6] = {1, 2, 3, 4, 5, 6}; // Will hold all even numbers
int ar2[6] = {7, 8, 9, 10, 11, 12}; // Will hold all odd numbers

for (i = 0, j = 0 ; rep < n/2 ; niter++)
{
if ((ar1[i]%2 == ODD) && (ar2[j]%2 == EVEN))
{
rep++;
swap = ar1[i];
ar1[i] = ar2[j];
ar2[j] = swap;

i++; j++;
}

else
{
if (ar1[i]%2 == EVEN)
i++;

if (ar2[j]%2 == ODD)
j++;
}
}

for (i = 0 ; i < n ; i++)
printf ("\n ar1[%d] = %d, ar2[%d] = %d", i, ar1[i], i, ar2[i]);

printf ("\nO(n) = %d", niter);

}


O(n) in this algo will be less than or equal to (2*n - 1)..

The code works for negative numbers also when compiled in ANSI C..

- Prax January 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code is in Java but you can get an idea from this. Time complexity O(n)

public static void sortEvenOdd(int[] arr1,int[] arr2){
		 int index1 =0;
		 int index2 =0;
		 int temp;
		 while((index1 = getNextOdd(index1,arr1)) != -1){
			 index2 = getNextEven(index2, arr2);
			 temp = arr1[index1];
			 arr1[index1] = arr2[index2];
			 arr2[index2] = temp;
		 }
	 }
	 public static int getNextOdd(int index,int[] arr){
		 for(int i=index;i<arr.length;i++){
			 if(arr[i]%2==1){
				 return i;
			 }
		 }
		 return -1;
	 }
	 public static int getNextEven(int index,int[] arr){
		 for(int i=index;i<arr.length;i++){
			 if(arr[i]%2==0){
				 return i;
			 }
		 }
		 return -1;
	 }

- Abdul Malik Mansoor February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rmComma(int a1[], int a2[])
{
int pOdd=0;
int pEven=0;

int length1 = 6;//sizeof(a1)/sizeof(int);//a1 supposed to store odd
int length2 = 6;// sizeof(a2)/sizeof(int);

while(pOdd<length1 && pEven<length2)
{
if(a1[pOdd]%2==0)
{
while(a2[pEven]%2!=1)
pEven++;
swap(a1[pOdd], a2[pEven]);

}
pOdd++;
}
}

- beyondfalcon April 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void SwapOddEven(int* odd, int* even,int sz){
int j=0;
for(int i=0;i<sz;++i){
if(odd[i]%2==0){
for(;j<sz;++j){
if(even[j]%2==1){
Swap<int>(odd[i],even[j]);
break;
}
}
}
}
}

- Anonymous June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i, j, temp, lastIndex;
int array1[10] = {-1,2,3,4,-5,6,7,8,9,10};
int array2[10] = {11,-12,13,14,15,-16,17,18,19,20};

for ( i = 0, lastIndex = 0; i < 10; i++)
{
if(array1[i] % 2 == 0)
{
temp = array1[i];//even number
for(j= lastIndex; j < 10 ; j++)
{
if(array2[j] % 2 == 1) //odd number
{
//exchange
array1[i] = array2[j];
array2[j] = temp;
lastIndex = j;
break;
}
}
}
}

- Anonymous October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have 2 pointers, one points at the first non-even number in the array of evens, the other at the first non-odd number in the array of odds. Swap the numbers and increase both pointers (separately) until they find 2 new numbers to be swapped. In-place and linear time. Java:

public static void arrange(int[] e, int[] o) {
	int we=0, wo=0;
	do {
		while (e[we]%2!=0) we++;
		while (o[wo]%2==0) wo++;
		int tmp=e[we];
		e[we]=o[wo];
		o[wo]=tmp;
	} while (wo<o.length&&we<e.length);
}

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

int a1[]={9,10,11,12,13,14,15,16};
		int a2[]={-1,-2,-3,-4,-5,-6,-7,-8};
		int i=0,j=0;
		while(i<8)
		{
			if(a1[i]%2==0)
			{
				if(a2[j]%2!=0)
				{
					int temp=a1[i];
					a1[i]=a2[j];
					a2[j]=temp;
					i++;
					j++;
				}
				else
					j++;
			}
			else
				i++;
		}
		for(i=0;i<8;i++)
			System.out.print(a1[i]+" ");
		System.out.println();
		for(i=0;i<8;i++)
			System.out.print(a2[i]+" ");

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

assumes that a1[] holds odd integers and a2[] holds even. Written in java. Works with negative numbers. Complexity O(n) since I pass through both arrays just once.

- Anonymous October 04, 2013 | Flag


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