Sorry to hear about the interview. It sucks... and it happens.
+1 for following up the question.
In the splitter function, I run through the "long string" with an index. Every iteration I use the String.substring method to extract that size of the interval from the string. (index + interval). After the extraction, I increment the index with the interval. Thus, moving through the long string, interval by interval.
It can happen that the index + interval is larger than the length of the long string. (will cause out of bound exception) Thus the extra check to avoid it, and save the remaining string.
public static void main(String[] args) {
String veryLongString = "12345678901234567890";
List<String> subStrings = splitter(veryLongString, 3);
// Print the result
for (String s : subStrings) {
System.out.print(s + " ");
}
}
public static List<String> splitter(String string, int interval) {
int index = 0;
List<String> subStrings = new ArrayList<String>();
while (index < string.length()) {
// Check if there is still enough characters to read.
// If that is the case, remove it from the string.
if (index + interval < string.length()) {
subStrings.add(string.substring(index, index + interval));
} else {
// Else, less the "interval" amount of characters left,
// Read the remaining characters.
subStrings.add(string.substring(index, string.length()));
}
index += interval;
}
return subStrings;
}
Output:
123 456 789 012 345 678 90