Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
private static int count = 0;
private static void arrange(String[] product, String[][] parameters,
int wInd) {
if (wInd == 0) {
// System.out.println("" + ++count + " " + Arrays.toString(prod));
System.out.print(++count + " ");
for (String p : product) {
System.out.print(p);
}
System.out.println();
} else {
for (int k = 0; k < parameters[wInd - 1].length; k++) {
product[wInd - 1] = parameters[wInd - 1][k];
arrange(product, parameters, wInd - 1);
}
}
}
public static void main(String[] args) {
String[] productName = new String[] { "shoes", "boot" };
String[] style = new String[] { "001", "002" };
String[] color = new String[] { "red", "blue" };
String[] size = new String[] { "7", "8", "9" };
String[] gender = new String[] { "m", "f" };
String[][] parameters = new String[][] { productName, style, color,
size, gender };
String[] product = new String[parameters.length];
arrange(product, parameters, product.length);
}
Writing loops will make the code static as the number of product options might change. Generally these are done with recursion. Am not sure about PHP but in python I will use a list of list and c i will use use an array of struct as data structures.
- kr.neerav June 25, 2014