## Amazon Interview Question

Software Engineer Interns**Country:**United States

**Interview Type:**In-Person

```
package gao.zone.study;
import java.lang.reflect.Array;
import java.util.Arrays;
public class SplitArr2 {
public static void main(String[] args) {
Integer[] arr = new Integer[88];// or use 0,1,19,20,21,999,1001
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
Integer[][] splits = split(arr, 20);
for (Integer[] sub : splits) {
System.out.println(Arrays.toString(sub));
}
}
/**
* Split.
*
* @param <T> the generic type
* @param arr the array, not null
* @param gap the gap distance, must be positive.
* @return the splitted arrays.
*/
@SuppressWarnings("unchecked")
public static <T> T[][] split(T[] arr, int gap) {
T[][] arrs = (T[][]) Array.newInstance(arr.getClass(),
Math.min(gap, arr.length));
int shortLength = arr.length / gap;
int reminder = arr.length % gap;
for (int i = 0; i < arrs.length; i++) {
int partLength = i < reminder ? (1 + shortLength) : shortLength;
arrs[i] = (T[]) Array.newInstance(arr.getClass().getComponentType(), partLength);
for (int j = i, k = 0; j < arr.length; j += gap, k++) {
arrs[i][k] = arr[j];
}
}
return arrs;
}
}
```

```
/**
* Put every nth element from given array to list
* @param input array to grab elements from
* @param nth every nth element will be selected from input
* @return a list with every nth element from input array
*/
private static <T> List<T> processArray(T[] input, int nth) {
List<T> list = new ArrayList<>();
for (int i=0, j=0; i < input.length; i++) {
if (j == nth - 1) {
list.add(input[i]);
j = 0;
} else {
j++;
}
}
return list;
}
/**
* Test
*/
public static void main(String[] args) {
Integer[] input = new Integer[1000];
// Fill array with sample objects
for (int i = 0; i < input.length; i++) {
input[i] = i + 1;
}
Integer[] result = processArray(input, 20).toArray(new Integer[0]);
for (Integer i : result) {
System.out.println(i);
}
}
```

I'm going say this sounds a lot more like a homework question than something an AMAZON interviewer would ask. The solution to this is 5 seconds and very much so beneath the level of developer they are looking for. If you can count by 20 you're done.

- Anonymous August 29, 2014Is there maybe more to the question?