Twitter Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
List<KeyVal> merge(listA, listB){
List<KeyVal> listC=new ArrayList<KeyVal>;
int i;
for(i=0;i<listA.size() && i<listB.size();i++)
{
if(listA.get(i).key==listB.get(i).key)
{
KeyVal pair= new KeyVal;
pair.key=listA.get(i).key;
pair.val=listA.get(i).val+listB.get(i).val;
listC.add(pair);
}else
{
if(listA.get(i).key<listB.get(i).key)
{
KeyVal pair= listA.get(i);
listC.add(pair);
} else
{
KeyVal pair= listB.get(i);
listC.add(pair);
}
}
}
if(listA.size()> = i)
{
for(int k=i; k<listA.size();k++)
{
listC.add(listA.get(k));
}
}
if(listB.size()> = i)
{
for(int k=i; k<listB.size();k++)
{
listC.add(listB.get(k));
}
}
}
Class KeyVal{
String key;
Integer val;
}
What? Explain it better please.
- bigphatkdawg September 12, 2013