## 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.

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

- nikhil19kekan December 04, 2018