Morgan Stanley Interview Question for Software Engineer / Developers






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

Sorting is O(nlogn) which is not good.
hash the first set. Then hash the second set and check if it already exist. If yes, then put the element in the intersection array.

- Avinash October 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please elaborate with pseudo code at least.

- Amit Petkar March 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If no duplicates then hashing is a good way to do !
but if there's duplicate then it will have some collision, then hashing will no longer be the best. I think consider this case, sort the small sets and do the binary search will be the best.

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

Sort the two set.... O(nlogn)
Compare from starting element:
If set one element is less than set two, go to next element in set one and vice versa.
If the two are equal, store in another array and go to next elements in both the sets.
This will take O(n) time. So the total complexity is still better than O(n2).

- Anon August 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

By set definition each set cannot have duplicates. Merge them together, sort, look for duplicates. The set of duplicates would be the intersection.

- Serguei May 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose we have, s1 = {a,b,c,d}, and s2={c,d,e,f}
Populate s1 in a hash table. e.g s1h, and check s2 elements in s1h, the elements in s2 that exists in s1h will be the intersection.

- Maninder Singh June 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

consider 2 sets of unequal sizes, A and B.

sort them in ascending order
assumed that since it is a set, there are no duplicates

num of elems in A = size(A)
num of elems in B = size(B)
create a 3rd array C with size = min(size(A), size(B)) // intersection cannot be greater than smallest array

while(int i < size(A)) //assuming size(A) < size(B)
while(int j < size(B))
if(A[i] < B[j])
i++
else if A[i] > B[j]
j++
else
copy A[i] into C
i++
j++

terminate when smaller array goes out of bounds, so dont need to check for all elements

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

this is just like the join in database, without index, hashing is definitely the best solution.

- mian December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Set<Long> a = new HashSet<Long>();
Set<Long> b = new HashSet<Long>();
Set<Long> intersection = new HashSet<Long>();

a.add(1L);
a.add(2L);
a.add(3L);
a.add(4L);

b.add(5L);
b.add(6L);
b.add(2L);
b.add(3L);

for(Long x : b){
if (!a.add(x)) {
intersection.add(x);
}
}

for(Long x : intersection){
System.out.print(x +" , ");
}

- Pranit April 08, 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