Facebook Interview Question for Software Developers


Team: Community Operations
Country: United States




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

const shiftK = (array, k) => {
    if (array.length < k){
        throw new Error("K should be less than array's length");
    }

    for (let i=0; i<k; i++){

        let temp = array[i];
        for (let j=i+k; j<array.length; j+=k){
            let newTemp = array[j];
            array[j] = temp;
            temp = newTemp;
        }

        array[i] = temp;
    }

    return array;
};

- vladimir.s December 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you dont need to throw error when k is greater than array size, since its the number of rotations you want to perform

- nikhil19kekan March 04, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def fun(temp,k,i,l):
	#swap l[i] and temp
	swap_val=l[i]
	l[i]=temp
	temp=l[i]
	if(temp==None):
		return l
	else:
		return fun(temp,k,(i+k)%len(l),l)

- nikhil19kekan December 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function(nums, k) {
    let len = nums.length
    let portion = nums.splice(len - (k%len), len)

    nums.unshift(...portion)
};

- dtallinterviewee December 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rotate(int in[], int l, int k){
	int cur = k%l;
	while(cur != 0){
		swap(in, 0, cur);
		cur = (cur+k)%l;
	}
}

- sushachawal December 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Whoops, forgot to include the swap function

void rotate(int in[], int l, int k){
	int cur = k%l;
	while(cur != 0){
		int temp = in[0];
		in[0] = in[cur];
		in[cur] = temp;
		cur = (cur+k)%l;
	}
}

- sushachawal December 08, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It won't work for the following input: in=[1,2,3,4,5,6], l=6, k=2.
The output of your function will be: [5,2,1,4,3,6], which is incorrect

- Anonymous December 15, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It won't work for the following input: in=[1,2,3,4,5,6], l=6, k=2.
The output of your function will be: [5,2,1,4,3,6], which is incorrect.

- user December 15, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It won't work for the following input: in=[1,2,3,4,5,6], l=6,k=2.
The output of your function will be: [5,2,1,4,3,6], which is incorrect.

- user December 15, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You're totally right! Thanks for pointing that out :) Will work on fixing that case

- sushachawal February 06, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

function rotate(inp, l, k){
  let cur = parseInt(l - (k%l));
  let i=0;
	while(cur < l){
		let temp = inp[i];
		inp[i] = inp[cur];
		inp[cur] = temp;
		cur++;
    i++;
	}
  cur = k;
  key = parseInt(l - (k%l));
  key2 = key;
  while(cur < key){
		let temp = inp[key2];
		inp[key2] = inp[cur];
		inp[cur] = temp;
		cur++;
    key2++;
	}
  console.log(inp);
}

let arr = [1,2,3,4,5,6]; 
rotate(arr,arr.length,2);

- maan December 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

function rotate(inp, l, k){
	let cur = parseInt(l - (k%l));
  let i=0;
	while(cur < l){
		let temp = inp[i];
		inp[i] = inp[cur];
		inp[cur] = temp;
    
		cur++;
    i++;
	}
  cur = k;
  key = parseInt(l - (k%l));
  let key2 = key;
  while(cur < key){
		let temp = inp[key2];
		inp[key2] = inp[cur];
		inp[cur] = temp;
		cur++;
    key2++;
	}
  inp = inp.filter(function( element ) {
     return element !== undefined;
  });
  console.log(inp);
}

let arr = [1,2,3,4,5,6,7]; 
rotate(arr,arr.length,2);

- maan December 12, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void rotate(int[] nums, int k) {
	       int n = nums.length;
	         if (n < 2)
	             return;
	        
	        k = k % n;
	        int count = 0;
	        for(int i = 0; count < n; i++){
	            int p = i;
	            int prev =nums[p];
	            do{
	                int j = (p + k) % n;
	               int temp = nums[j];
	                nums[j] = prev;
	                prev = temp;
	                p = j;
	                count++;
	            }while( p != i);         
	        }
	    }

- shirforum December 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int[] rotateArrayConstantSpace2(int[] arr, int k) {
		if(k <= 0) {
			return null;
		}
		int temp;
		for(int i = 0, j = arr.length - k; i <= k-1 && j < arr.length; i++, j++) {
			temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
		return arr;
	}

- Anil Bhaskar January 10, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What complexity it gives? is it on(n) as k is constant

- Question March 06, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] MoveLastToFirstInArray(int[] array, int k)
        {
            if (k < 0 || array == null || !array.Any())
            {
                return null;
            }

            var length = array.Length;

            if (k > length)
            {
                return null;
            }

            var i = k; 
            var j = 0;

            var newArray = array.Reverse().ToArray();

            while (i < length)  
            {
                newArray[i] = array[j]; 

                i++;
                j++;
            }

            return newArray;
        }

- Elena January 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int [] swapAll_recur(int []arr,int k,int start)
{

if(start>=arr.length-1)
{
return arr;
}
// swap the kth element in the result right sub arry
for(int i=start;i<start+k;i++)
{
int temp=arr[i];
arr[i]=arr[arr.length-k+(i-start)];
arr[arr.length-k+(i-start)]=temp;
}
return swapAll_recur(arr, k,start+k);

}

- Mohamed Hegazy February 05, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

{{ int []arr={1,2,3,4,5,6,7};
arr= swapAll_recur(arr,2,0);
for(int i=0;i<arr.length;i++)
{
System.out.println("arr["+i+"]:"+arr[i]);
}
}}

- engmmhegazy February 05, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its simple rotate. Just traverse from back. Find the point by reducing index by k. P
Then reverse from 0 to p and p+1 to n-1
Thrn reverse whole array

- nitinguptaiit March 31, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Swift

let k = 2
        var l = [1,2,3,4,5,6]
        for _ in 0..<k {
            for i in stride(from: l.count - 1, to: 0, by: -1){
                if i > 0 {
                    l[i - 1] ^= l[i]
                    l[i] ^= l[i - 1]
                    l[i - 1] ^= l[i]
                }
            }
        }
	print(l)

- PeterResp May 12, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] l = new int[]{1,2,3,4,5,6};
        int k = 2;
        int len = l.length;
        int cutIndex = len - k;
        for (int i = 0; i < cutIndex; i++) {
            l[k + i] = (i%(k + i)) + 1;
            if (i < k) {
                int itMove = l[i] + cutIndex;
                l[i] = itMove;
            }
        }
        System.out.println(Arrays.stream(l).mapToObj(it -> String.valueOf(it)).collect(Collectors.joining(", ")));

- batman87 July 12, 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