Originally posted by: ones3k
Originally posted by: DrPizza
I posted this in Highly Technical as well:
Something smells fishy...
ones3k keeps bumping this thread.
Over a week ago, he asked the same question in highly technical as question #1
People immediately suspected it was a request for homework help. He never responded to that accusation.
Now, that same question has been posed, along with other questions, along with the source of the questions: a Google interview.
Anyone think it's odd he didn't mention the interview a week ago?
Nor has he posted a complete correct solution to any of the problems.
Furthermore, he's asked that we post complete solutions for any of the problems for everyone else to see.
ones3k, maybe you're being honest about this, and if so, I apologize.
However, I still smell a plea for homework help.
I'll post the solution to #1:
here it goes:
My solution is to create a binary tree with each leaf representing a stream. We construct the tree from the leaves up. Each node in the tree will contain an integer.
First we insert all K leaves and their corresponding integers (which will be the current element in each stream). From here we construct a "tournament" tree by pairing up nodes at each level and taking the smallest. At the root of the tree we will have the smallest item. Before we move on, please draw this incase im being confusing.
Next, notice that the smallest item (root), must of been compared against the second smallest.
So we increment the stream corresponding to our winning element and update our tree by pushing the new element through the same path as the old element. This is a log(k) operation and we are guaranteed to get the smallest every time because the smallest element beat the second smallest.
so its O(k) to set up the tree and access the first element and O(log(k)) for each stream access thereafter