## Facebook Interview Question

Software Developers**Team:**Community Operations

**Country:**United States

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

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;
}
}
```

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

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.

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.

```
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);
```

```
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);
```

```
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;
};
```

```
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;
}
```

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);

}

- nikhil19kekan December 04, 2018