## Facebook Interview Question

Software Developers**Team:**Community Operations

**Country:**United States

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

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

}

```
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(", ")));
```

- vladimir.s December 20, 2018