Question:
Given n Sets of choices: (1, 2, 3), (A, B, c), (I, II, III).
You pick one element from each set of choices. Generate all possible pickings.
http://www.glassdoor.com/Interview/ Given-n-sets-of-choices-1-2-3-2-3-4-4-5-you-pick-one-element-from-each-set-of-choices-generate-all-possibl-qtn_ 218899.htm
Private list<list<string>> generateallpickings (String[][] pool) { List<List<String>> result = new ArrayList<> (); help (Pool, 0, new arraylist<string> (), result); return result;} Private void help (string[][] pool, int group, List<String> cur, List< List<string>> result) { if (Cur.size () == pool.length) { result.add (new ArrayList< string> (cur)); } if (group >= pool.length) { return; } // now we are looking at pool[group], put each elements into the cur for (int i = 0 ; i < pool[group].length ; i ++) { cur.add (Pool[group][i]); Help (Pool, group + 1, cur, result); Cur.remove (Cur.size () - 1); }}
[Twitter] All possible pickings