Walmart Labs Interview Question for SDE1s


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 2 vote

O(n^2). pseudo code:

for (a in A) {
   for (b in B) {
       insert(a+b, hashtable);
   }
}

for (b in B) {
    for (c in C) {
        if (b+c in hashtable) {
            // blah
        }
    }
}

- SheezFrebreeze September 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SPACE: N^2
COMPLEXITY: N^2

package com.test.src;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
 * Given 3 unsorted arrays A, B and C you need to find all possible combinations
 * such that A[i] + B[j] = B[k] + C[l].
 * 
 * - venkataratnamkumar7777 11 months ago in United States | Report Duplicate |
 * Flag |
 * 
 * 
 * @author agupta13
 *
 */
public class FourArraySumEqualityFinder {

	public static void main(String... args) {
		List<Integer> A = Arrays.asList(args[0].split("")).stream().map(s -> Integer.parseInt(s))
				.collect(Collectors.toList());
		List<Integer> B = Arrays.asList(args[1].split("")).stream().map(s -> Integer.parseInt(s))
				.collect(Collectors.toList());
		List<Integer> C = Arrays.asList(args[2].split("")).stream().map(s -> Integer.parseInt(s))
				.collect(Collectors.toList());

		System.out.println(combinationFinder(A, B, C));
	}

	// A[i] + B[j] = B[k] + C[l]
	private static Integer combinationFinder(List<Integer> A, List<Integer> B, List<Integer> C) {
		Integer combinations = 0;
		Map<Integer, Integer> sumOfABMap = new HashMap<>();
		Map<Integer, Integer> sumOfBCMap = new HashMap<>();
		for (Integer a : A) {
			for (Integer b : B) {
				Integer sum = a + b;
				Integer counter = 0;
				if (sumOfABMap.containsKey(sum)) {
					counter = sumOfABMap.get(sum) + 1;
				}
				sumOfABMap.put(sum, counter);
			}
		}

		for (Integer b : B) {
			for (Integer c : C) {
				Integer sum = b + c;
				Integer counter = 0;
				if (sumOfBCMap.containsKey(sum)) {
					counter = sumOfBCMap.get(sum) + 1;
				}
				sumOfBCMap.put(sum, counter);
			}
		}
		
		for(Entry<Integer, Integer> eSetAb : sumOfABMap.entrySet()) {
			if(sumOfBCMap.containsKey(eSetAb.getKey())) {
				System.out.println("eSetAb.getKey()="+ eSetAb.getKey());
				combinations += eSetAb.getValue() * sumOfBCMap.get(eSetAb.getKey());
			}
		}
		
		return combinations;
	}
}

- ABHAY August 19, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<int>vec;
unordered_set<int>s;
for(int i=0;i<A.length();i++)
for(int j=0;j<B.length();j++)
if(s.find(A[ i ]+B[ j ])==s.end( ) )
s.insert(A[ i ]+B[ j ]);

for(int i=0;i<C.length();i++)
for(int j=0;j<B.length();j++)
if(s.find(C[ i ]+B[ j ]) !=s.end( ) )
vec.push_back(C[i]+B[ j ]);

return vec;
//conplexity:O(N^2)

- bikash August 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Something like this should work

for (int i=0; i < a.length; i++)
    for (int j=0; j < b.length; j++)
        for (int k=0; k < b.length; k++)
            for (int l=0; l < b.length; l++)
                if (a[i] + b[j] == b[k] + c[l])
                    System.out.println(String.format("a[%d] : %d +b[%d] : %d == b[%d] : %d +c[%d] : %d",
                            i, a[i], j, b[j], k, b[k], l, c[l]));

- PeyarTheriyaa September 22, 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