8x8 Interview Question for Applications Developers


Country: United States




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

public int depthSum(const List<NestedInteger>& nestedList) {

sum = 0;
std::stack<std::pair<NestedInteger, int>> listStack;
listStack.push(std::make_pair(listStack, 1);

do {
const auto [currentList, depth] = listStack.pop();
for (const auto& listItem : currentLsit) {
if (listItem.isInteger() {
sum += depth * currentList.isInteger();
} else {
listStack.push(std::make_pair(listItem, depth + 1));
}
}
} while (!listStack.empty());

return sum;
}

- v^2 June 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class NestedWeightedSum {
    static int sum =0;
	static int boolean shouldUseBFS = false;
	static int boolean shouldUseDFS = true;
	
	public void bfs(List<NestedInteger> nestedList) {
	    Queue<NestedInteger> queue = new LinkedList<>();
        for (NestedInteger nestedInt : nestedList) {
            queue.offer(nestedInt);
        }
        
        int depth = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            depth++;
            for (int i = 0; i < size; i++) {
                NestedInteger nestedInt = queue.poll();
                if (nestedInt.isInteger()) {
                    sum += nestedInt.getInteger() * depth;
                } else {
                    for (NestedInteger innerInt : nestedInt.getList()) {
                        queue.offer(innerInt);
                    }
                }
            }
        }
	}
    public int depthSum(List<NestedInteger> nestedList) {
         if (nestedList == null || nestedList.size() == 0) {
            return 0;
        }
		if(shouldUseBFS){
			bfs(nestedList);
		}
		if(shouldUseDFS){
			dfs(nestedList,0);
		}
		return sum;
        
    }

    public void dfs(List<NestedInteger> nestedList,int depth){
        for(NestedInteger list: nestedList){
            if(list.isInteger()){
                sum += list.getInteger() * depth;
            }
            else {
                dfs(list.getList(),depth + 1);
            }
        }
    }
}

- sachinsharma31261 June 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks for the Answers !

- Tekfiesta June 24, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

func sum(of array: [Any], level: Int = 1) -> Int {
    guard array.isEmpty == false else { return 0 }

    var runningSum = 0

    for item in array {
        if let intValue = item as? Int {
            runningSum += intValue * level
        } else if let array = item as? [Any] {
            runningSum += sum(of: array, level: level + 1)
        } else {
            assertionFailure("Unexpected/Unhandled type")
        }
    }

    return runningSum
}

sum(of: [1,[4,[6]]])

- Anonymous June 25, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Repeat of [ careercup.com/question?id=5139875124740096 ]

- NoOne June 27, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def SumAndAns(input_list):
sum, ans = 0, 0
for i in input_list:
if isinstance(i, list):
sum_tmp, ans_tmp = SumAndAns(i)
sum += sum_tmp
ans += ans_tmp
else:
sum += i
ans += i
return sum, ans

- Anonymous June 28, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

JavaScript

const sumOfNestedList = function(arr) {
  let sum = 0;
  const add = function(arr, weight) {
    for (let i = 0; i < arr.length; ++i) {
      if (!Array.isArray(arr[i])) {
        sum += arr[i]*weight;
      } else {
        add(arr[i], ++weight);
        --weight;
      }
    }
  }
  add(arr, 1);
  return sum;
}

- Terence Ng June 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

JavaScript

const sumOfNestedList = function(arr) {
  let sum = 0;
  const add = function(arr, weight) {
    for (let i = 0; i < arr.length; ++i) {
      if (!Array.isArray(arr[i])) {
        sum += arr[i]*weight;
      } else {
        add(arr[i], ++weight);
        --weight;
      }
    }
  }
  add(arr, 1);
  return sum;
}

- Terence Ng June 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Too much convolution, really. Not needed.

def depth_sum( some_item , cur_depth = 0){
   if ( !(some_item isa 'list') ) return ( some_item * cur_depth ) 
   sum ( some_item ) as {
       depth_sum( $.o, cur_depth + 1 )
   }
}
A = [[1,1],2,[1,1]]
x = depth_sum( A )
println(x)

- NoOne June 30, 2020 | 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