Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

en.wikipedia.org/wiki/External_sorting#External_merge_sort

Read one item at a time from each input stream and put them in a min-heap.

- oOZz June 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please provide some code snippet?
Also why do you need a min-heap?
Shouldn't the external merge sort be enough?

- Subhajit June 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

@Subhajit I was thinking of two different approaches. One of them is external sorting and it is clear to understand (see wiki for details). This is a good solution, if you read the entire streams in to a file/disk first and then merge them. What if though, you're doing the external sorting and meanwhile you have still numbers coming in from the input streams non-stop?

The other approach is sorting the numbers as they are coming in from multiple input streams and they may be coming asynchronously. For this, you need to find a way to sort the numbers as they are coming in in different speeds. A heap is a perfect data structure for asynchronous streams to sort the streaming elements. The single insert operation is O(log n) and you can build the heap in O(n log n) time from multiple streams for n numbers.

Design a stream data structure (interface) that is "iterable" by numbers for your input streams, which will have let's say hasNext() and next() method implementations. For instance for Java, this is like implementing the Iterable interface:

public interface MyStreamInterface<Integer> implements Iterable<Integer> {}

All of your input stream classes will implement this interface. Then you can have two threads sharing a min-heap, one for reading from the streams and inserting them into the min-heap (Producer) and the other one writing from the min-heap to a file (Consumer):


Producer:

for (MyStreamInterface stream: streams) {
		if (stream.hasNext()) {
			minHeap.insert(stream.next());
		}
}

Consumer:

while (!minMeap.isEmpty()) {
	file.writeInt(minHeap.extractMin())
}

Of course, writing to and reading from the shared min-heap has to be synchronized as in Producer-Consumer problem.

- oOZz June 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I like the heap idea and the Iterator idea. The whole merge could be also wrapped as Iterator:

public class MergeIterator implements Iterator<Integer> {
  public MinHeap h = new MinHeap();
  
  class Node { int v; Iterator<Integer> i; }
  
  public MergeIterator(Collection<Iterator<Integer>> inputs) {
    for(Iterator<Integer> i : inputs) {
      if(i.hasNext()) 
        h.insert(new Node(i.next(), i));
    }
  }
  public boolean hasNext() { return ! h.isEmpty(); }
  public Integer next() {
    Node min = h.extractMin();
    if(min.i.hasNext()) 
      h.insert(new Node(min.i.next(), min.i));
    return min.v;
  }
}

- galli August 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, why don't we use TreeSort, which sorts automatically, when the input data comes in and its also a Dynamic Array.

- prashanthreddy.mtech June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

N = input sources

int A[N];

for( int i = 0; i < N; ++i )

{

    int ret = readNextValue( i, &A[i] );

    if( ret == NO_MORE_INPUTS )

    {

        A[i] = MAX_INT;

    }

}

int min_index;

do

{

    min_index = 0;

for( i = 1; i < N; ++i )

{

    if( A[i] < A[min_index] )

    {

        min_index = i;

    }

}   

    if( A[min_index] != MAX_INT )

{

writeIntoSortedArray( A[min_index] );

int ret = readNextValue( min_index, &A[min_index] );

    if( ret == NO_MORE_INPUTS )

    {

        A[min_index] = MAX_INT;

    }

}

} while( A[min_index] != MAX_INT );

- Mukesh June 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question here is asking for an API design and makes references to Object Oriented and accepting different types of streams.

This is much less about external sorting techniques and more creating a reasonable interface.

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

k-way merge will work only if we are able to read one input from all the k input streams and store it in a min-heap ,
but will fail if k is very very large and memory is less to accomodate even a k-sized heap.

- Pras July 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import heapq

class HeapNode:
def __init__(self, value, collIndex):
self.value = value
self.collIndex = collIndex

def __eq__(self, other):
return self.value == other.value
def __lt__(self, other):
return self.value < other.value
def __gt__(self, other):
return self.value > other.value

class MergeIterator:
def __init__(self, sources):
self.sources = sources
self.sourcesCount = len(sources)
self.minHeap = []

for i in range(0, self.sourcesCount):
current = self.sources[i]
if len(current) > 0:
node = HeapNode(current.pop(0), i)
heapq.heappush(self.minHeap, node)

def hasNext(self):
return len(self.minHeap) > 0

def next(self):
if not self.hasNext():
return None
min = heapq.heappop(self.minHeap)
collection = self.sources[min.collIndex]
if len(collection) > 0:
node = HeapNode(collection.pop(0), min.collIndex)
heapq.heappush(self.minHeap, node)
return min.value

- rchg1988 July 24, 2015 | 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