VMWare Inc Interview Question
Member Technical StaffsCountry: United States
Interview Type: Phone Interview
public class CharNumberAI {
public static void Do()
{
Map<Integer, List<String>> listHashMap = new HashMap<Integer, List<String>>();
List<List<String>> inputList=new ArrayList<List<String>>();
List<String> arrayList=null;
arrayList=new ArrayList<String>();
arrayList.add("a1");
arrayList.add("b1");
inputList.add(arrayList);
arrayList=new ArrayList<String>();
arrayList.add("a2");
arrayList.add("b2");
inputList.add(arrayList);
arrayList=new ArrayList<String>();
arrayList.add("a3");
arrayList.add("b3");
inputList.add(arrayList);
PrintPowerSet(inputList, inputList.size()-1, new HashSet<String>());
}
private static void PrintPowerSet(List<List<String>> allchars, int index, Set<String> permSet)
{
List<String> stringList = allchars.get(index);
Set<String> subPremSet = null;
for(String str :stringList)
{
subPremSet=new HashSet<String>();
subPremSet.addAll(permSet);
if (index == 0)
{
subPremSet.add(str);
printSet(subPremSet);
}
else
{
//Console.Write(ch);
subPremSet.add(str);
PrintPowerSet(allchars, index - 1, subPremSet);
}
}
}
public static void printSet(Set<String> hashSet){
Iterator<String> i=hashSet.iterator();
System.out.print("{ ");
while(i.hasNext()){
System.out.print(i.next()+", ");
}
System.out.println("}");
}
}
public static List<ArrayList<String>> getPermutations(
List<ArrayList<String>> lists) {
List<ArrayList<String>> currentPermutations =
new ArrayList<ArrayList<String>>();
// generate new permutations one list at a time
for (ArrayList<String> list : lists) {
currentPermutations =
generatePermutations(currentPermutations, list);
}
return currentPermutations;
}
private static List<ArrayList<String>> generatePermutations(
List<ArrayList<String>> currentPermutations,
ArrayList<String> list) {
// we'll be creating a whole new set of permutations from
// currentPermutations and the 'list' of new items
List<ArrayList<String>> newPermutations =
new ArrayList<ArrayList<String>>();
for (String item : list) {
if (currentPermutations.isEmpty()) {
// first generated list - just add the single items
ArrayList<String> newPermutation = new ArrayList<String>();
newPermutation.add(item);
newPermutations.add(newPermutation);
}
else {
// create a new permutation list based
// on the old list plus our new item
for (ArrayList<String> curList : currentPermutations) {
if (!curList.contains(item)) {
ArrayList<String> newPermutation =
new ArrayList<String>(curList);
newPermutation.add(item);
newPermutations.add(newPermutation);
}
}
}
}
return newPermutations;
}
public static void getPermutationsTest() {
List<ArrayList<String>> permutations =
new ArrayList<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
permutations.add(list);
list = new ArrayList<String>();
list.add("b1");
list.add("b2");
list.add("b3");
permutations.add(list);
list = new ArrayList<String>();
list.add("c1");
list.add("c2");
permutations.add(list);
permutations = Util.getPermutations(permutations);
for (ArrayList<String> permutation : permutations) {
System.out.println(permutation);
}
}
// Output:
// [a1, b1, c1]
// [a2, b1, c1]
// [a1, b2, c1]
// [a2, b2, c1]
// [a1, b3, c1]
// [a2, b3, c1]
// [a1, b1, c2]
// [a2, b1, c2]
// [a1, b2, c2]
// [a2, b2, c2]
// [a1, b3, c2]
// [a2, b3, c2]
Same, but a bit less code
public static Set<Set<String>> getSetPermutations(List<List<String>> lists) {
final HashSet<Set<String>> acc = new HashSet<Set<String>>();
getSetPermutations(new ArrayList<List<String>>(lists),
new HashSet<String>(), acc);
return acc;
}
private static void getSetPermutations(List<List<String>> lists,
Set<String> current,
Set<Set<String>> acc) {
if (lists.isEmpty()) {
acc.add(current);
return;
}
List<List<String>> listsLocal = new ArrayList<List<String>>(lists);
for (String s : listsLocal.remove(0)) {
final HashSet<String> set = new HashSet<String>(current);
set.add(s);
getSetPermutations(listsLocal, set, acc);
}
}
package kb.learning;
import java.util.ArrayList;
import java.util.List;
public class Combinations
{
static List<List<Character>> lists = new ArrayList<>();
private static void fillInput()
{
List<Character> l = new ArrayList<>();
l.add('A');
l.add('B');
l.add('C');
lists.add(l);
l = new ArrayList<>();
l.add('1');
l.add('2');
l.add('3');
lists.add(l);
l = new ArrayList<>();
l.add('a');
l.add('b');
l.add('c');
lists.add(l);
}
public static void main(String[] args)
{
fillInput();
generateCombo("", 0);
}
private static void generateCombo(String beginStr, int listIndex)
{
if(listIndex == lists.size() - 1)
{
for(Character c : lists.get(listIndex))
System.out.println(beginStr + c);
}
else
{
for(Character c : lists.get(listIndex))
generateCombo(beginStr + c, listIndex + 1);
}
}
}
package com.fb.inter;
import java.util.ArrayList;
import java.util.List;
public class Q1 {
public static void main(String[] args){
List<List<String>> permutations =
new ArrayList<List<String>>();
ArrayList<String> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
permutations.add(list);
list = new ArrayList<String>();
list.add("b1");
list.add("b2");
list.add("b3");
permutations.add(list);
list = new ArrayList<String>();
list.add("c1");
list.add("c2");
permutations.add(list);
printPermutations(permutations);
}
private static void printPermutations(List<List<String>> lists){
int numOfPerms = getNumOfPerm(lists);
int numOfLists = lists.size();
int[] indices = new int[numOfLists];
List<List<String>> permutations = new ArrayList<List<String>>(numOfPerms);
for(int i = 0; i < numOfPerms; i++ ){
List<String> onePerm = new ArrayList<String>();
for(int k = 0; k < indices.length ; k++){
onePerm.add(lists.get(k).get(indices[k]));
}
permutations.add(onePerm);
increaseIndices(indices, lists);
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < permutations.size(); i++ ){
builder.append('[');
List<String> currentPermutation = permutations.get(i);
for(int j = 0; j < currentPermutation.size(); j++ ){
builder.append(currentPermutation.get(j)).append(',');
}
builder.append(']');
System.out.println(builder.toString());
builder.setLength(0);
}
}
private static void increaseIndices(int[] indices, List<List<String>> lists){
boolean done = false;
for(int i = 0; i < indices.length && ! done ; i++ ){
indices[i] = (indices[i] + 1) % lists.get(i).size();
done = indices[i] != 0;
}
}
private static int getNumOfPerm(List<List<String>> lists){
int total = 1;
for(List<String> list: lists){
int tempTotal = (list != null && list.size() != 0) ? list.size() : 1;
total *= tempTotal;
}
return total;
}
}
public class PermutationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arrayOne = {
"a1", "b1", "c1","d1"
};
String[] arrayTwo = {
"a2", "b2", "c2"
};
String[] arrayThree = {
"a3", "b3", "c3"
};
List<List<String>> result=new ArrayList<List<String>>();
List<String> list=null;
for(String l1Elem:arrayOne){
for(String l2Elem:arrayTwo){
for(String l3Elem:arrayThree){
list=new ArrayList<String>();
list.add(l1Elem);
list.add(l2Elem);
list.add(l3Elem);
result.add(list);
}
}
}
for(List<String> resultElem:result){
System.out.println();
for(String s:resultElem){
System.out.print(s+":");
}
}
}
}
I don't see the need for all this code. I think the following is sufficient.
public static void main (String args[]){
String []set1= {"a1","b1","c1","d1"};
String []set2= {"a2","b2","c2"};
String []set3= {"a3","b3","c3"};
for (int i=0; i<set1.length; i++){
for (int j=0;j<set2.length; j++){
for (int k=0; k<set3.length; k++){
System.out.println(String.format("{%s,%s,%s}",set1[i],set2[j],set3[k]));
}
}
}
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void permutation(vector<string> a,vector<string> b, vector<string> c)
{
for(vector<string>::iterator i=a.begin()+1;i<a.end();i++)
{
for(vector<string>::iterator j=b.begin()+1;j<b.end();j++)
{
for(vector<string>::iterator k=c.begin()+1;k<c.end();k++)
{
cout<<*i<<"\t"<<*j<<"\t"<<*k<<endl;
}
cout<<endl;
}
cout<<endl;
}
}
int main()
{
vector<string> a,b,c;
int n;
string in;
cin>>n;
for(int i=0;i<=n;i++)
{
getline(cin,in);
a.push_back(in);
}
cin>>n;
for(int i=0;i<=n;i++)
{
getline(cin,in);
b.push_back(in);
}
cin>>n;
for(int i=0;i<=n;i++)
{
getline(cin,in);
c.push_back(in);
}
permutation(a,b,c);
}
public static void permuteList(String[][] list, int start, ArrayList<String> perms){
if(start == list.length){
if(perms.size() == list.length)
System.out.println(perms.toString());
return;
}
for(int i = 0; i < list[start].length; i++){
perms.add(list[start][i]);
for(int j = start+1; j <= list.length; j++){
permuteList(list, j, perms);
}
perms.remove(list[start][i]);
}
}
public static void permuteList(String[][] list, int start, ArrayList<String> perms){
if(start == list.length){
if(perms.size() == list.length)
System.out.println(perms.toString());
return;
}
for(int i = 0; i < list[start].length; i++){
perms.add(list[start][i]);
for(int j = start+1; j <= list.length; j++){
permuteList(list, j, perms);
}
perms.remove(list[start][i]);
}
}
rehash
- S O U N D W A V E October 17, 2013careercup.com/question?id=6318635089920000
collabedit.com/4pmvt