Linkedin Interview Question for Software Engineer / Developers






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

Assuming the arrays are sorted in ascending order.

1. Create a Min Heap of size n.
2. Insert every 1st element of the arrays into heap.
3. Get the top element out of the heap and pop it out (print/store it).
4. Keep a track of the array from which the popped element came from and insert the next element from that array only.
4. reheap/heapfy the heap.
5. repeat 2-4 untill the element count in heap becomes zero.

- Paras February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good answer but I think you should use a priority queue. Store the array from which each element came and set the value of the element as the priority.

- Code Saviour February 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

He did use a heap, which is an implementation of priority queue.

- ftfish February 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1 for Paras and ftfish

- Second Attempt March 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Concur..

Strategy is to ignore how big the data is and deal with it in a controlled way.

Min or Max heap (depending on sorting order) with initial pull from the first (top) element in each stream.

Whenever the top of heap is extracted, pull another top from the same stream.
- some extra pointer here needed to associate the heap element back to the parent data stream.

- fydango March 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

something similar to N-Way merge?

- Anonymous February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'd assume that, unless this was too far along in the interview process to possibly be that simple. My association with 'streams' is that they're lazy, ie on-demand. So, every time you get a 'demand' for an item, walk the fronts of the non-empty streams, keeping track of the lowest item and where it was. At the end, produce that lowest item (taking care to remove it from its stream).

- Guy Smiley April 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tournament tree data structure

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

but how is heap going to help if it's an infinite stream? even that will require memory. Actually I didn't get the question well.. What is significance of the stream being "infinite"? how exactly we delimit the stream into workable subsets etc.. ?

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

Hi, you wont be storing infinite elements in the heap.
"Insert every 1st element of the arrays into heap" -> means only n elements would be in the heap at any time

- viv February 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since it is a stream, it probably means that we can read it sequentially. Also the problem says that stream is sorted so the first element in a stream is smaller than the second element. The solution seems simple:
1. Read first element from stream 1
2. Read first element from stream 2
3. Compare the two and write the smaller one to output stream keeping the other one with us.
4. If the element that was written to stream was from stream1 pick the next element from stream1 else pick from stream2 and compare with the element with us in step 3 and write the smaller to output stream.
5. Repeat
It is kinda like merge-sort

- Sandy September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Would the approach of simply running through both the streams work? I did not understand the advantage of using a mi heap. If in the end we are combining the elements from both the list, why not simply traverse each list simultaneously. eg

if(a[I] < b[j]

insert a[I]

I++

else insert b[j]

j++

- Anonymous November 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question is with 'n' streams which is a finite value, & implicitly means that you can store those many values.. Only problem is that, you cannot store these values all in one go, as they are infinite stream ==> that cannot be contained by your RAM size.... Now if you know, how min-heap works, the first answer is closest one till now

- KaLee June 01, 2016 | 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