Facebook Interview Question
Software DevelopersTeam: 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(", ")));
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)
}
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)
}
//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;
}
#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;
}
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] + " ");
}
}
- vladimir.s December 20, 2018