Facebook Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Using Binary search you should look for the first V[i] that is lower than i+1.
Assume the array with indices 0..n is sorted with the greatest number on the left. Then look in the middle and if V[i] < i+1 then memoize this as a minimum found and look in the left side of the rest of array, otherwise don't memoize and look in the right side until you will get to array of length 1. Then the result is memoized-1.

- Demo April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Implemented above algo and using binary search

public static int getHIndex2(Integer[] arr){
		
		Arrays.sort(arr,Collections.reverseOrder()); // O(nLog(n))
		int h = 0;
		
		int low=0;
		int high=arr.length-1;
		
		//log(n)
		while(low<=high){
			int mid = (low+high)/2;
			if(arr[mid] >= mid+1){
				h=mid+1;
				low=mid+1;
			}else{
				high=mid-1;
			}		
		}
		return h;
	}

- Mayank Jain August 14, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int hIndex(int V[], int len) {
	int h = 0;
	for(int i=0; i<len; i++) {
		if(V[i]>=i+1)
			h=i+1;
	}
	return h;
}

int main() {
	int N[] = {1, 1, 1, 1, 1, 1, 1, 1};
	printf("H Index: %d\n", hIndex(N, 8));
	return 0;
}

- Anil April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Though this works perfectly, I guess interviewer is looking for something better as the array is sorted.
BST approach works better.

- bharat April 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

What is h index and what is its application??

- Somu April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We know that if we order the array from largest values to smallest values, that we can keep basically incrementing h values until we get to a number that is smaller than its current index- Since bigger numbers have more flexibility in being considered in the h-index count.

public int getH(int[] arr) {
	
	int h = 0;

	for (int i = 0; i >= 0 && arr[i] >= i + 1; i--) {

		if (arr[i] >= i + 1) {

			h++;
		}
	}

	return h;
}

- SK April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You have to find an index i , such that all indexes from 0 to i have ValueAt(i) >=i. Since array is sorted this can be done with binary search in log(n) time.

#include<iostream>
#include<stdlib>

using namespace std;


int findHIndex(int * a, int start, int end)
{
	int mid = start+end /2;
	if(a[mid] == mid) //last point for H index search
		return mid;
	if(mid == start)
		return -1;

	else if (a[mid] > mid) // h is to the right of this point
	{
		findHIndex(a,mid+1,end);
	}
	else //h is to the left of this point
	{ 
		findHIndex(a,start,mid-1);
	}
		
}

- legolas April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done with binary search in log n time. Find the index where a(mid) >= mid and
a(mid+1) <=mid.

- legolas April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What do you mean by minimum? Isn't the h-index unique?

- Yawn.Zheng April 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Solution based on binary search approach. Time complexity O(log n)
        public static void hIndex(int[] array) {
		int length = array.length;
		int h = hIndex(array, 0, length-1, 0);
		System.out.print(h);
	}
	
	public static int hIndex(int[] array, int start, int end, int h) {
		if(start > end)
			return h;
		
		int mid = (start+end)/2;
		if(array[mid] >= array.length-mid) 
			return hIndex(array, start, mid-1, array.length-mid);
		else
			return hIndex(array, mid+1, end, h);
	}

- rit April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int search_hindex(int *input, int start, int end, int hindex)
{
	int half =0, found = 0;
	if (start > end)
		return hindex;
	half = start + (end - start)/2;

	if (half + 1 > input[half])
	{	
		hindex = half + 1;
		found= search_hindex(input, start, half -1, hindex);
	} else {
		found = search_hindex(input, half+1, end, hindex);
	}
	return found;
}

int compute_hindex(int* input, int length)
{
	int hindex = 0; //not found
	hindex = search_hindex(input, 0, length-1, hindex);
	if (hindex ==0)
		hindex = length;
	else
		hindex--;

	return hindex;
}

- hankm2004 June 22, 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