Facebook Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

the naive way to sort the array in descending order, the best complexity would be O(n*log(n)), quick sort would be fine for that and then just return Kth element.
The second thought is using the standard top-N algorithm to build the the min-heap of size K+1 from the input array, the heap will contain K+1 largest elements from the input. Then return the heap top which is Kth largest.
The complexity is O(n*log(K) ), the space is O(K+1)

- S.Abakumoff February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"standard top-n algorithm"? Well seems to be not so standard to Google, but anyway, just look for "C++ partial_sort implementation", it does the very same thing. And in more detail.

1) Add elements to the heap until it has size K
2) Then you always remove the min element which is log(K) in exchange for adding a LARGER element. This way you get all the (n-k) smallest elements out of there...
3) Return the head of the min-heap.

So you get O(log(K) * (n-k) + k) for a pairing/fibbo-heap. Not all heaps will give you this worst-case performance... So a binary heap would end up with O(log(K) * (n-k) + k*log(k)) = O(n*log(k))...

Alternatively you could use randomized Quickselect, which to me sounds nicer... and gives you a more stable complexity of O(n) (randomized).

- FBNerd March 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

we can do using median of medians.time complexity O(kn)

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

This may help you..

int partition(int* a, int low, int high)
{
/*Start index for scanning the array*/
int left = low;
/*Select middle element of the array as pivot*/
int pivotIdx = low + (high - low)/2;
/*Swap pivot with right most element of the array*/
int pivot = a[pivotIdx];
a[pivotIdx] = a[high];
a[high] = pivot;
pivotIdx = high;
/*Pivot will be placed at this index such that all elements
to right of it are greater than pivot and all elements to
the left of it are smaller than pivot*/
int partitionIdx = low;
while (left < high)
{
/*Initially our partition index is set to leftmost element
index in the input array. We keep traversing towards right
of the input array and if we find an element lesser than
pivot, we swap it with element at partiotion index and
increment partition index else just keep moving right*/
if (a[left] < pivot)
{
int tmp = a[left];
a[left] = a[partitionIdx];
a[partitionIdx] = tmp;
++partitionIdx;
}
++left;
}
/*Place the pivot value at the partition index*/
a[pivotIdx] = a[partitionIdx];
a[partitionIdx] = pivot;
return partitionIdx;
}

/*K is the kth largest element to find. Initial call
to this function should be called with 0 and size-1 for
low and high respectively.*/
int quickSelect(int* a, int low, int high, int k)
{
assert(a);
assert(k <= high+1);
if (low == high)
return a[low];
int pivotIdx = partition(a, low, high);
int sizeOfLeftSubArray = pivotIdx - low + 1;
/*If'pivotIdx' is greater than 'k', keep looking towards
left part*/
if (sizeOfLeftSubArray > k)
{
return quickSelect(a, low, pivotIdx-1, k);
}
/*If'pivotIdx' is less than 'k', keep looking towards
right part*/
else if (sizeOfLeftSubArray < k)
{
return quickSelect(a, pivotIdx+1, high, k-sizeOfLeftSubArray);
}
/*We just found our kth index, return it*/
else
{
return a[pivotIdx];
}
}

- Anonymous September 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

wikipedia selection algorithm

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

standard selection algorithm is optimal, but impractical.

heap (implemented as array) can be O(n log K) time and O(K) space, which can be pretty fast for smallish K.

Interesting to discuss would be representation of reals...

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

import heapq
testk = 2
array = [3,6,5,8,7,9,1,10,20,18,50,40]

k = testk + 1
heap = array[0:k]
heapq.heapify(heap)

for i in array[k:]:
    if i > heap[0]:
        heapq.heapreplace(heap,i)
print heap[0]

- Kaede February 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For this problem, we could try to use quicksort multiple times.
First choose a pivot A[i], then put the doubles that is less than A[i] on its left, the doubles bigger than A[i] on its right. Then check the number of doubles are bigger than A[i], name it m, if m>K, we throw away the part that is less or equal to A[i], K is unchanged;
if m==K, we return A[i]
if m<K, we throw away the part that is bigger than A[i], update K to K-m. Then we do the same thing to the remaining array :choose pivot and sort around that pivot

The time complexity is O(n)+O(n/2)+O(n/4)+.....=O(n) in the average case. Worst case senario would be O(n^2)though.
Space complexity is O(n)

- genier February 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it can be done in two way:-
1)either sort the array then take the kth element from last.space complexity 0(1) time complexity o(nlogn).
2)take first k element of array and make min heap from that,for the rest element check the value of root of heap with array value if array value > root value,replace the root value with array value.space complexity is o(k) which is constant time is o(klogk)+(n-k)log(k).

- go4gold February 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No one mentioned quickselect ? Expected complexity O(n)
Here's is a java solution :

import java.util.Random;


public class KLargest {
	static double [] tab;
	static Random rand = new Random();
	
	public static double largest(double [] intab,int k){
		tab=intab;
		return largestAux(k, 0, tab.length-1);
	}
	
	private static double largestAux(int k, int start, int end)
	{
		int pivot = rand.nextInt(end-start+1)+start;
		int res = partition(start,end,pivot);
		
		if (res-start == k)
			return tab[res];
		else if(res-start <k)
			return largestAux(k-(res-start+1),res+1,end);
		else
			return largestAux(k,start,res-1);
	}
	
	private static int partition(int start, int end, int pivot){
		double val = tab[pivot];
		int insert = start;
		swap(pivot,end);
		for(int i=start;i<=end;i++){
			if(tab[i]>=val){
				swap(i,insert);
				insert++;
			}
		}
		return insert-1;
	}
	
	private static void swap(int i, int j){
		double temp = tab[i];
		tab[i] = tab[j];
		tab[j]=temp;
	}
	
	public static void main(String [] args){
		System.out.println(largest(new double[]{1,2,3,4,5},2));
	}
}

- TonyStack June 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

This question is a good test for the underlying understanding of tradeoff.

1> For Memory Compromise, I can use a Hash of k buckets to hold k different values which has a time advantage of O(k) but memory O(k)
2> For Time Compromise, I will use any sorting perhaps the merge sort which can provide the best of all giving O(nlogn) . (I m talking about Merge function without using extra additional array ).

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

ok.

- S.Abakumoff February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wow What is this ?? Am I reading it correct ?? Can't we find kth largest using hash ?? if so why the hash is used for ??

- hprem991 February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok, man, let's play this game:)

- S.Abakumoff February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry man. if I disappointed ya..coz its not abt what u think its abt what is right.. So if U hv any trouble in understanding or hving better idea post it.. Don't take it personal..

Anyway.. lemme know if U donno how to find kth largest using Hash.. Coz Hash basically is used to find values with condition and here the condition is the largest value..

- hprem991 February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

k^th largest using hash? I call BS.

Given a random set of objects which support a Compare function, give an algo using hash, to find the k^th largest.

Go on...

- Anonymous February 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are certain psyco person like this "Anonymous" who does not even have enough gud to express his identity, ruining the technical discussion and moving down the correct idea at the bottom of the discussion.

I urge the users to just ignore the non-technical views and provide your own analysis.. I am going to report this mis conduct

- hprem991 February 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL @hprem991.

First, anonymous users cannot vote down. So, your answer was voted down by someone who has registered in this site, a brave person, gasp.

Second, all I see in a comment by Anonymous is a technical question, which you don't seem to have understood (or are trying to dodge). It is a very valid question to your hash claim.

Perhaps you consider the BS comment rude? Welcome to the internet.

- Anonymous February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting. You claim you can do this using a hash but give no example :P. Great. Must be a cool hash function that orders your values. Even if it would exist, it would still create a log(n)n algorithm... Because either the isnert or retrival of your hashmap can't be O(1).

And mergesort is by far not the best sorting algorithm and especially not the inplace version which is a huge mess in itself.

- RandomGuy February 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Use quick select algorithm O(n) time

blog.teamleadnet.com/2012/07/quick-select-algorithm-find-kth-element.html

- thelineofcode July 24, 2013 | 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