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 votes

only work when array.length%k==0

- lazycat October 20, 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
Comment hidden because of low score. Click to expand.
0
of 0 vote

void shift(vector<int>& a, size_t k) {
  if (k == 0) return;
  if (k >= a.size()) throw runtime_error("Invalid argument: k");
  auto p = prev(a.end(), k);
  reverse(a.begin(), p);
  reverse(p, a.end());
  reverse(a.begin(), a.end());
}

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

Java solution:

public static int [] rotateArray(int [] arr, int k){
        for(int i = 0; i < k; i++){
            int j = arr.length-1, last = arr[j];
            while (j>0)
                arr[j]=arr[--j];
            arr[0] = last;
        }
        return arr;
    }

Time complexity: O(nk);

- ahmedkhaled September 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def r_rotate(l):
    temp = l[-1]
    for i in range(len(l)-1, 0, -1):
        l[i] = l[i-1]
    l[0] = temp

def rotate(l, k):
    #if len(l) == 1:
    #    return

    for i in range(k):
        r_rotate(l)


l = [1,2,3,4,5,6]
rotate(l, 2)
print(l)

l2 = [1]
rotate(l2, 2)
print(l2)

- yuhechen2018 March 10, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Am I misreading what is supposed to be done here?
Why are some solution accepting an array and two integers?

- Robert April 06, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 2 3 4 5 6
5 2 3 4 1 6
5 6 3 4 1 2
5 6 1 4 3 2
5 6 1 2 3 4

- Anonymous June 12, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

fun Array<Int>?.rotateBy(by: Int, start: Int = 0) {
fun Array<Int>.swap(i : Int, j : Int) {
val temp = this[i]
this[i] = this[j]
this[j] = temp
}
if (this == null || isEmpty()) {
return
}
val rotate = by % size
if (rotate <= 0) {
return
}
var i = start
var j = size - rotate
val stop = j
while (i < stop) {
if (j == size) {
j = stop
}
swap(i++, j++)
}
rotateBy(size - j, i)
}

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

fun Array<Int>?.rotateBy(by: Int, start: Int = 0) {
    fun Array<Int>.swap(i : Int, j : Int) {
        val temp = this[i]
        this[i] = this[j]
        this[j] = temp
    }
    if (this == null || isEmpty()) {
        return
    }
    val rotate = by % size
    if (rotate <= 0) {
        return
    }
    var i = start
    var j = size - rotate
    val stop = j
    while (i < stop) {
        if (j == size) {
            j = stop
        }
        swap(i++, j++)
    }
    rotateBy(size - j, i)
}

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

//time complexity o(k*n) space complexity o(1)

#include<iostream>
#include<vector>

using namespace std;

int main(){

int siz,k;
cout << "\n Size : ";
cin >> siz;
vector<int> obj(siz);

cout << "\nValues : \n";

for(int& i : obj)cin >> i;

cout << "\n Enter The Value For K : ";
cin >> k;

int x=0;

while(x<k){

int last=obj[obj.size()-1];

for(int i=obj.size()-2;i>=0;i--){
obj[i+1]=obj[i];
}

obj[0]=last;

x++;

}

cout << "\n Reordered or rotated array is as follows : (as per o(1) space complexity constraint) \n [ ";

for(auto i : obj){
cout << i << " ";
}

cout << "] \n";

return 0;
}

- singaram July 22, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<vector>

using namespace std;

int main(){

int siz,k;
cout << "\n Size : ";
cin >> siz;
vector<int> obj(siz);

cout << "\nValues : \n";

for(int& i : obj)cin >> i;

cout << "\n Enter The Value For K : ";
cin >> k;

int x=0;

while(x<k){

int last=obj[obj.size()-1];

for(int i=obj.size()-2;i>=0;i--){
obj[i+1]=obj[i];
}

obj[0]=last;

x++;

}

cout << "\n Reordered or rotated array is as follows : (as per o(1) space complexity constraint) \n [ ";

for(auto i : obj){
cout << i << " ";
}

cout << "] \n";

return 0;
}

- singaram July 22, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
        int[] a = {1,2,3,4,5,6};
        int[] res = new int[a.length];
        int k = 3;
        int mod = k%(a.length-1);
        int i=0;
        int index = mod;
        do{
            res[index++] = a[i++];
            if(index>a.length-1) index=0;
        }while(index != mod && i<a.length);
        i =0;
        for(i=0;i<res.length;i++){
            System.out.print(res[i] + " ");
        }
    }

- Akash Deep April 26, 2022 | 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