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

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

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

}

- vladimir.s December 20, 2018