Goldman Sachs Interview Question
Software Engineer / DevelopersI have never tried this, but i think this approach works:
1) Write two java classes which has threads t1, t2 in each of them respectively.
2) t1- will create an expandable array (Vector) which will take 10000 elements a time.
3) t2- will search for the element in the created Vector.
4) when t1 is executing, t2 searches for the element in the created Vector.
This may fasten up things. Let me know if there will be any problem with this approach.
This likely won't work very well, for a bunch of reasons:
1) resizing the vector will be costly.
2) if the threads are running on threads in different sockets, the caches are separate. Thread t1 is not really don't anything all that useful as all its work is producing data in the wrong place (cache line misses will be high for t2).
3) t1 could easily examine the entries as it puts them in the vector, probably for little/no overhead.
A good parallel implementation should have threads operating on distinct pieces of the data. For example, t1 could look in the first 500000 integers while t2 looks at the others. The two sets of integers could be stored in two structures by the two threads, using any of the techniques shown above.
Again, all answers.. no question to ask. If I'm an interviewer, I will minus you guys points because you guys are making ASSumpations. (The poster all need to be clear)
Questions to ask:
Are integers repeated?
Are all integers distinct?
Do you know how many are repeated?
Are integers sequential? (if they are, you don't need stupid fancy btrees and crap)
Are integers in order? (if they are, you don't need stupid fancy btrees and crap)
Are they all random?
Knowing the above will efficienize the method to find it based on the appropriate "algorithm"
It's an overkill to do hashtable and crap if it's sequential.
The question is too easy to be straightforward (oh bsort, bsearch, btree, blah). Interviewer is looking if you are ASSuming. I'm an interviewer, btw, but.. I don't use 1 million.. I use 100 integers and see the questions they try to clarify..that's the real world.
B+ trees (treat the numbers as keys to a table in a database).
- Anonymous January 03, 2010