## Facebook Interview Question

Java Developers**Country:**United States

```
String[] staggerArrays(String[] input) {
String[] output = new String[input.length];
int j = 1;
for (int i = 0; i < input.length; i = i + 3) {
// The one in comments work too
// output[3*(j-1)] = input[i];
// output[3*(j-1) + 1] = input[i+1];
// output[3*(j-1) + 2] = input[i+2];
output[i] = "a" + j;
output[i + 1] = "b" + j;
output[i + 2] = "c" + j;
j++;
}
return output;
}
```

```
String[] staggerArrays(String[] input) {
String[] output = new String[input.length];
int j = 1;
for (int i = 0; i < input.length; i = i + 3) {
// The one in comments work too
// output[3*(j-1)] = input[i];
// output[3*(j-1) + 1] = input[i+1];
// output[3*(j-1) + 2] = input[i+2];
output[i] = "a" + j;
output[i + 1] = "b" + j;
output[i + 2] = "c" + j;
j++;
}
return output;
}
```

```
import java.util.Arrays;
public class StaggerArrays {
public static void main(String[] args) {
String array1[] = new String[]{"a1", "a2", "a3","a4"};
String array2[] = new String[]{"b1", "b2", "b3","b4"};
String array3[] = new String[]{"c1", "c2", "c3","b4"};
String[] staggerArray = new String[array1.length * 3];
for (int i=0,j =0;i < staggerArray.length; i+=3,j++) {
staggerArray[i] = array1[j];
staggerArray[i + 1] = array2[j];
staggerArray[i + 2] = array3[j];
}
System.out.println(Arrays.asList(staggerArray));
}
}
```

```
//var strArray = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3", "d1", "d2", "d3"]
var strArray = ["a1", "a2", "b1", "b2", "c1", "c2"]
func firstLetterOf(_ string: String) -> Character? {
return string.first
}
func sortArray(_ array: inout [String]) -> [String] {
guard !array.isEmpty,
let firstLetter = firstLetterOf(array[0]) else {
return array
}
// calculate number of letters in one group
var numberOfLettersInGroup = 0
for group in array {
guard let letter = firstLetterOf(group), letter == firstLetter else {
break
}
numberOfLettersInGroup += 1
}
// sorting algorithm
let numberOfGroups = array.count / numberOfLettersInGroup
var currentIndex = 0
var p = 0
for i in 0..<numberOfLettersInGroup-1 {
let multiplicator = numberOfLettersInGroup - i
for j in 0..<numberOfGroups {
let removingIndex = j * multiplicator + p
//
print("removing from: \(removingIndex)")
let element = array.remove(at: removingIndex)
//
print("inserting to: \(currentIndex)")
array.insert(element, at: currentIndex)
//
currentIndex += 1
print(array)
}
p = currentIndex
}
return array
}
sortArray(&strArray)
```

//considering we were give the value of N and array of size 3N.

//input ['a1,'a2,'a3','b1','b2','b3','c1','c2','c3'], N= 3

//output ['a1,’b1,’c1,’a2’,’b2’,’c2’,’a3’,’b3’,’c3']

public String[] getStaggeredArray(String[] arry, int n) {

String[] result = new String[arry.length];

if ( (3*n) != arry.length) {

System.out.println("Something is wrong");

}

int counter = 0;

for(int i =0; i < n; i++) {

result[counter++] = arry[i];

result[counter++] = arry[(i+n)];

result[counter++] = arry[(i+(2*n))];

}

return result;

}

// Both time complexity and space complexity O(n)

We can ignore the input and and re-populate the array using division and modulo by (array.length/nLetters).

- Hagai C February 10, 2018If we don't know how many letters (#{a,b,c} = 3), we can first scan the input to find that out.