Facebook Interview Question for Software Engineers


Country: United States




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

All the algo questions were seen except the Random Max Index. Optimal solution is O(n) in time (single pass)and constant extra space.

Generate random max index
Given an array of integers, randomly return an index of the maximum value seen by far.

e.g.
Given [11,30,2,30,30,30,6,2,62, 62]

Having iterated up to the at element index 5 (where the last 30 is), randomly give an index among [1, 3, 4, 5] which are indices of 30 - the max value by far. Each index should have a ¼ chance to get picked.

Having iterated through the entire array, randomly give an index between 8 and 9 which are indices of the max value 62.

Solution:

public void sampleIdx(int[] array){
        if(array == null || array.length == 0){
            return;
        }
        Random rnd = new Random();
        int res = 0, max = Integer.MIN_VALUE, count = 0;
        for(int i = 0; i < array.length; i++){
            if(max < array[i]){
            	max = array[i];
                res = i;
                count = 1;
            }else if(max == array[i]){
                count++;
                int idx = rnd.nextInt(count); //(0, k - 1)
                if(idx == 0){

                    res = i;
		        System.out.print(“A max value index up to the ”+i +”th element is ” + res;
);

                }
            }
        }
    }

Looking for interview experience sharing and mentors?
Visit A++ Coding Bootcamp at aonecode.com.

Taught by experienced engineers/interviewers from FB, Google and Uber,
our ONE TO ONE courses cover everything in an interview including
latest interview questions sorted by companies,
SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)
ALGORITHMS (conquer DP, Graph, Greed and other advanced algo problems),
and mock interviews.

Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.

Welcome to email us with any questions. Thanks for reading.

- aonecoding April 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python solution for decoding ways:

def decodeWays(ints):
        if not ints or ints.startswith('0'): return 0
        dp = [1 for i in xrange(len(ints)+2)]

        for i in xrange(len(ints)):
            if ints[i] == '0':
                if int(ints[i-1]) < 1 or int(ints[i-1]) > 2: 
                    return 0
                dp[i+2] = dp[i]
            else:
                dp[i+2] = dp[i+1]
                if 10 < int(ints[i-1:i] + ints[i]) < 27:
                    dp[i+2] += dp[i]
        return dp[-1]

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

Python solution for converting to double linked list:

def doConvert(node, isLeft=False):
    if not node:
        return node
    
    node.prev = doConvert(node.left, True)
    node.next = doConvert(node.right, False)
    
    if node.prev: node.prev.next = node 
    if node.next: node.next.prev = node
     
    return node.next or node if isLeft else node.prev or node

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

finding random max index: start at random index, go over all elements in search for the max value (loop the index using modulo) and return the first occurrence of the max

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

Random Index C++ O(n) time and O(n) space

int randomIndex(vector<int>& nums)
{
	int size = nums.size();
	
	if(size == 0)
		return -1;
	if(size == 1)
		return 0;

	vector<int> indices;
	int max = INT_MAX;

	for(int i=0;i<size;i++)
	{
		if(nums[i] > max)
		{
			indices.clear();
			max = nums[i];
			indices.push_back(i);		
		}
		else if(nums[i] == max)
		{
			indices.push_back(i);
		}
	}

	if(indices.size() == 1)
		return 0;

	int rand = rand();
	int idx_size = indices.size();

	return indices[rand % idx_size];

}

- MSD May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Two loops:
First count how many times the max appears. Let's say it appears 5 times. Then pick a random number from 1-5 (say it was 3)
Second loop, pick 3rd maximum number

- RP June 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

thanks, do you have any advice on data engineer interview as well ?

- nidhi April 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hi Nidhi,

Thanks for the inquiry. A few of our mentors are experienced backend data engineers. They can definitely guide you on the data architecture/warehousing area of the interviews. Please feel free to write to us aonecoding@gmail.com with any further questions.

Thanks!

- aonecoding April 18, 2017 | 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