Goldman Sachs Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
public int getListsCount(List list){
int count = 0;
for (int i=0; i <list.size(); i++ ){
if (list.get(i) instanceof Object){
count = count + 1 + getListsCount((List)list.get(i));
}
}
return count;
}
}
public class ParentList {
List myPrivateList= new ArrayList();
public int getNumberOfList(List list)
{
for(Object i: list)
{
if(i instanceof List)
{
count++;
getNumberOfList((List)i);
}
}
return count+1;//for the main list
}
}
/*A List PARENTLIST which contains primitive type and List L, this List L further can have primitve type and List L and so on.
* Given a Root node calculate how many List it have
* */
public static int listCount(List l){
int count = 0;
for (Object object : l) {
count++;
if(object instanceof List)
listCount((List)object);
}
return count;
}
You should see linked list implementation in the linux kernel. This question seems to be parallel to how does iteration over list list_for_each() is implemented.
- rahul.deshmukhpatil May 30, 2013