Facebook Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

public void sort(int[] arr){
        int startIndex = 0;
        int distance = 1;
        int[] newArr;
        for(int i=0;i<arr.length;i++){

            if(i+1 != arr.length && arr[i] >arr[i+1]){
                distance++;
            }else{

                if(distance>1){
                    newArr = new int[distance];
                    for(int j=0;j<distance;j++){
                        newArr[j] = arr[startIndex+j];
                    }
                    int[] revArr = revArr(newArr, distance);

                    for(int j=0;j<distance;j++){
                        arr[j+startIndex] = revArr[j];
                    }
                    i=-1;
                    startIndex=0;
                    distance=1;
                }else{
                    startIndex++;
                }
            }
        }
    }

- Chaitanya Srikakolapu November 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I assume, the question is to sort the entire array by using ONLY the given reverse method
Input: arr = [2,3,1,5,4]
Output arr = [1,2,3,4,5]
Available method = void reverse(int[] arr, int k)

Restrictions:
1. not allowed to modify the reverse method

Possible solution:
1. Since the method can only reverse elements, one possible solution is to sort the array like a bubble sort O(N^2) by reversing the out of order elements

For example:
Input: [2,3,1,5,4]

Step1: Check index 0 and 1. They are in increasing order, so no reverse call required
Step2: Check index 1 and 2. They are out of order, so call reverse method and pass only 2 elements from the array with k = 2
and so on

Input: [2,3,1,5,4]
Step1: Check 2 and 3. They are in order so change required ==> [2,3,1,5,4]
Step2: Check 3 and 1. They need to be sorted. Call reverse ([3,1], 2) ==> [2,1,3,5,4]
Step3: Check 2 and 1. They need to be sorted. Call reverse ([2,1], 2) ==> [1,2,3,5,4]
Step4: check 1 and 2. They are in order so change required ==> [1,2,3,5,4]
Step5: check 2 and 3. They are in order so change required ==> [1,2,3,5,4]
Step5: check 3 and 5. They are in order so change required ==> [1,2,3,5,4]
Step6: check 5 and 4. They need to be sorted. Call reverse ([5,4], 2) ==> [1,2,3,4,5]

Entire array is sorted.

- Saurabh November 19, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function reverse(arr, k) {
let totalOp = Math.floor(k / 2);

for (let i = 0; i < totalOp; i++) {
let newIndex = k - 1 - i;
if (arr[i] < arr[newIndex]) {
continue;
}

// swap
let temp = arr[newIndex];
arr[newIndex] = arr[i];
arr[i] = temp;
}

return arr;
};

- Anonymous November 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function reverse(arr, k) {
	let totalOp = Math.floor(k / 2);

	for (let i = 0; i < totalOp; i++) {
		let newIndex = k - 1 - i;
		if (arr[i] < arr[newIndex]) {
			continue;
		}

		// swap
		let temp = arr[newIndex];
		arr[newIndex] = arr[i];
		arr[i] = temp;
	}

	return arr;
};

- Anonymous November 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function reverse($arr, $k){
		$midvalue = floor($k/2);

		for($i = 0; $i <= $midvalue; $i++){
			$lasindex = $k - $i -1;

			if($arr[$i] < $arr[$lasindex]){
				continue;
			}

			$temp = $arr[$i];
			$arr[$i] =  $arr[$lasindex];
			$arr[$lasindex] = $temp;
		}
		return $arr;
	}

- KKhaing December 01, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php 	function reverse($arr, $k){
		$midvalue = floor($k/2);

		for($i = 0; $i <= $midvalue; $i++){
			$lasindex = $k - $i -1;

			if($arr[$i] < $arr[$lasindex]){
				continue;
			}

			$temp = $arr[$i];
			$arr[$i] =  $arr[$lasindex];
			$arr[$lasindex] = $temp;
		}
		return $arr;
	}

	$arr = [2,3,1,5,4];
	$res = reverse($arr, 3);

	print "[". implode(",",$res) ."]";?>

- KKhaing December 01, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function reverse($arr, $k){
		$midvalue = floor($k/2);

		for($i = 0; $i <= $midvalue; $i++){
			$lasindex = $k - $i -1;

			if($arr[$i] < $arr[$lasindex]){
				continue;
			}

			$temp = $arr[$i];
			$arr[$i] =  $arr[$lasindex];
			$arr[$lasindex] = $temp;
		}
		return $arr;
	}

	$arr = [2,3,1,5,4];
	$res = reverse($arr, 3);

	print "[". implode(",",$res) ."]";

- kathy December 02, 2019 | 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