Bloomberg LP Interview Question for Software Engineer / Developers






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

Why not use the generic algorithms for set in STL?

const int SET_SZ = 12;
const int iArray1 [12] = { 100, 150, 200, 250, 225, 300,
325, 125, 400, 175, 600, 50 };
const int iArray2 [12] = { 275, 300, 325, 350, 375, 400,
75, 25, 450, 575, 600, 550 };

set<int> s1( iArray1, iArray1 + SET_SZ );
set<int> s2( iArray2, iArray2 + SET_SZ );
set<int> sr;

cout << "Computed intersection: " << endl;
set_intersection( s1.begin(), s1.end(),
s2.begin(), s2.end(),
ostream_iterator<int>(cout, ",") );

cout << endl << "Computed Union: " << endl;
set_union( s1.begin(), s1.end(),
s2.begin(), s2.end(),
ostream_iterator<int>(cout, ",") );

cout << endl << "Computed Difference: " << endl;
set_difference( s1.begin(), s1.end(),
s2.begin(), s2.end(),
ostream_iterator<int>(cout, ",") );

- STL Answer February 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is O(nlogn). Specific implementation is not a point here. We are discussing the best (fastest, using least memory) answer possible. Anyway, nice piece of code! :)

- ~ February 21, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Bucket or hash array 1 using any standard indexing mechanism and use this index against 2nd array

- bnp July 19, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bnp, do you mind elaborating?

- cb July 31, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use merge sort, but just keep in mind while merging the sub arrays take only those values which are uncommon

- Ani August 11, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Complexity O(nlogn) ... Google onsite question

- Ani August 11, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O( n log n ) is too slow. bnp's solution is better.

- been there September 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

I meant this to be a response to Ani's comment. I have asked this question at Amazon and Microsoft and an O( n log n ) solution doesn't cut it.

Might be OK at Google though ;-)

- been there September 01, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was asked same question for yahoo phone interview.....He was not satisfied with merge sort solution as it will require more space also....he was expecting solution without sorting so I think BNP solution is good fit for it. But more explanation is awaited...

- sp101 September 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BNP can u elaborate please?

- Java Coder November 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

store array 1 and array 2 as a hashed array using the same standard function. O(n)

take indexes of array 1 to get the keys from array 2. O(n)

If it exists then there is a match i.e. intersection

- fish December 03, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If buffer is allowed, then hash table will do. 0(n)

If no buffer is allowed, I can only think of merge sort. Any better idea?

- Lindos February 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If someone could explain, Hows does the Hashtable / Hashset method work in O(n)?

Is this the correct implementation for that?

import java.util.*;

public class hash
{
public static void main( String[] args )
{
HashSet<String> set1 = new HashSet<String>();
set1.add( "ape" );
set1.add( "cat" );
set1.add( "bat" );

HashSet<String> set2 = new HashSet<String>();
set2.add( "bat" );
set2.add( "fox" );
set2.add( "ape" );

System.out.println( "1 = " + set1 + ", 2 = " + set2 );


set1.retainAll(set2); //Keep entries which exist in both
System.out.println( "Intersection = " + set1);

}
}

- Ronin February 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
Treeset is better for numbers as it sorts at insertion stage {{{ int[] arr1 = {1,2,3,4,5,6}; int[] arr2 = {5,6,7,8,9}; int[] arr3 = new int[arr1.length+arr2.length]; TreeSet h1 = new TreeSet(); for(int i=0;i<arr1.length;i++) { h1.add(arr1[i]); } for(int i=0;i<arr2.length;i++) { h1.add(arr2[i]); } Object[] obj = h1.toArray(); int[] intObj = new int[obj.length]; for(int i=0;i<obj.length;i++) { intObj[i] = (Integer)obj[i]; System.out.println(intObj[i]); } - Shree March 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For Array1, Sort it using any Sorting mechanism and for Array2, use binary search for getting the intersaction.

- Puranik June 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,
I think we all are missing the word "distinct" here. Using hash table one can find intersection but then additional check is required to find distinct intersection. Also with merge sort we need to modify the algorithm to have only distinct elements in each array.

- k v August 02, 2008 | 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