Facebook Interview Question for Software Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

This can be solved using a stack, by pushing all elements until a closing bracket is encountered, then you start to pop the elements until you hit a starting bracket then multiply the result by the order of the nested brackets (3,2,1), then push that result into the stack until the whole array of characters is processed.

- MA November 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the input is not given as a string its rather list of lists

- nikhil19kekan November 30, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@MA you should write steps here. Its confusing what you mentioned.

- karansoft8 December 08, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Element {
    int val;
    List<Element> list;
    boolean isList;
}

 private long getValue(Element e, int factor) {
        long sum = 0;
        if(e == null)
            return sum;
        if(!e.isList)
            return factor * e.val;
            
        for(Element item : e.list) {
            sum += getValue(item, factor + 1);
        }
        return sum;
    }

- Popeye November 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct NestedList{
    bool isList;
    int val;
    vector<NestedList> internal;

    NestedList(int v):isList(false),val(v){
    }

    NestedList(const vector<NestedList> &v):isList(true),internal(v){}
};

int nestedListSum(const vector<NestedList> &v,int multFactor){
    if(v.empty()){
        return 0;
    }

    int sum = 0;
    for(auto &l : v){
        if(l.isList){
            int res = nestedListSum(l.internal,multFactor + 1);
            sum += res*multFactor;
        }else{
            sum += multFactor*l.val;
        }
    }

    return sum;
}

int nestedListSum(const vector<NestedList> &v){
    return nestedListSum(v,1);
}

- Guy November 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Sum {
	public static int calculate(List<Object> a, int multiplier) {
		int sum = 0;
		for (int i = 0; i < a.size(); i++) {
			if (a.get(i) instanceof ArrayList) {
				List l = (ArrayList) a.get(i);
				if (l.size() >= 1) {
					multiplier++;
					sum += multiplier * ((int) calculate(l, multiplier));
				}
			} else {
				sum += ((int) a.get(i));
			}
		}
		return sum;
	}

	public static void main(String[] args) {
		List<Object> list  = new ArrayList<>();
        
       		list.add(8);
        	list.add(3);
        	list.add(2);
        
        	List<Object> inner1 = new ArrayList<>();
        	inner1.add(5);
        	inner1.add(6);
        
        	List<Object> inner2 = new ArrayList<>();
        	inner2.add(9);
        	inner1.add(inner2);  
        
        	list.add(inner1);
        
        	list.add(6);
        
        	System.out.println(calculate(list, 1));
	}
}

- Levi November 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
private static void addSum(List in) {
int res = addSumHelper(in, 0 , 1);
System.out.println(res);
}

private static int addSumHelper(List in, int sum, int level) {
for (Object list : in) {
if(list instanceof List)
sum += addSumHelper((List)list, 0, level+1);
else
sum += (Integer)list;
}
return sum*level;
}
}}

- Anonymous November 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

l=[8,3,2,[5,6,[9]],6]
count=1
ans=0
def fun(l,count,ans):
    for e in l:
        if(type(e) is list):
            ans=fun(e,count*(count+1),ans)
        else:
            ans+=count*e
    return ans
ans=fun(l,count,ans)
print(ans)

- nikhil19kekan November 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Javascript Solution
{{
var input = [8,3,2,[5,6,[9]],6];

var mySum = function(inputX, level){
var result = 0;
var temp;
level = level + 1;
for(var i = 0; i < inputX.length; i++){
if(Array.isArray(inputX[i])){
temp = mySum(inputX[i], level);
result = result + (level * (temp));
}else{
result = result + inputX[i];
}
}
return result;
}

var result = mySum(input, 1);
console.log(result);
}}

- Anonymous December 03, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

type NType = (number | NType)[];
function add(what : NType) {
   let value = 0;
   let last = Number.NaN;
   for (let e of what) {
      if (typeof e == "number") {
         value += last;
         last = e;
      } else {
         if (Number.isNaN(last))
            last = 1;
         last = last * add(e);
      }
   }
   if (Number.isNaN(last)) {
      assert(value == 0);
      return 0;
   }
   return value + last;
}

- xiongmao December 08, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More