## Amazon Interview Question for Software Engineers

Country: United States
Interview Type: In-Person

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

This is a 'merge set' question. Given a graph, figure out which nodes belong to the same connected component and put them into a set.
Since the input comes in as an edge set, UNION FIND will be a good way to solve this.

Initially every node sources to itself. As we read the statement X = Y, we point the source of Y to the source of X so that they join the same set. After all connected components are sorted out. We check the unequal statements X != Y. If any of the X, Y pairs do share the same source, then X != Y contradicts with the equal statements.

``````public class CheckStatements {
public boolean validStatements(char[][] equal, char[][] unequal) {
int[] sets = new int[26];
for(int i = 0; i < 26; i++) {
sets[i] = i;
}

for(char[] pair: equal) {
mergeSets(sets, pair[0] - 'A', pair[1] - 'A');
}
for(int i = 0; i < 26; i++) {
findSrc(sets, i);
}

for(char[] pair: unequal) {
if(sets[pair[0] - 'A'] == sets[pair[1] - 'A']) return false;
}
return true;
}

private int findSrc(int[] sets, int i) {
int src = i;
while(src != sets[src]) {
src = sets[src];
}
int tmp;
while (i != sets[i]) {
tmp = sets[i];
sets[i] = src;
i = tmp;
}
return src;
}

private void mergeSets(int[] sets, int a, int b) {
int srcA = findSrc(sets, a);
int srcB = findSrc(sets, b);
sets[srcB] = srcA;
}
}``````

Looking for interview questions sharing and mentors? Visit A++ Coding Bootcamp at aonecode.com (select english at the top right corner).
We provide ONE TO ONE courses that cover everything in an interview from the latest interview questions, coding, algorithms, system design to mock interviews. All classes are given by experienced engineers/interviewers from FB, Google and Uber. Help you close the gap between school and work. Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.
Welcome to email us with any questions.

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

``````public bool CheckStatements(char[,] seta, char[,] setb){

List<HashSet<char>> setGroups = new List<HashSet<char>>();

int len_seta_row = seta.GetLength(0);

Dictionary<char, char> map = new Dictionary<char, char>();

// Group the first set into the groups.
for(int i = 0; i < len_seta_row; i++){

// if both the char's are new, then add both to dictionary.
if(!map.ContainsKey(seta[i, 0]) && !map.ContainsKey(seta[i,1])){
}else if (map.ContainsKey(seta[i,0]) && !map.ContainsKey(seta[i,1]))
{
//if  B = C, then map.Add('C', 'A');
var key = seta[i,1];
var value = map[seta[i,0]];
}else{ //if D = C, then map.Add('D', 'A');
var key = seta[i,0];
var value = map[seta[i,1]];
}

}

//Check for the setb if the pairing is correct.

int len2_setb_row = setb.GetLength(0);

for(int i = 0; i < len2_setb_row; i++){

if(map[setb[i,0]] == map[setb[i,1]]){
return false;
}
}

return true;
}``````

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

public class ValidateEquations {

private static int[] parent = new int[27];

private static int[] rank = new int[27];
static {
for (int i = 0; i < 26; i++)
parent[i] = i;
}

public static int getParent(int x) {
if (parent[x] != x)
parent[x] = getParent(parent[x]);

return parent[x];
}

public static void union(int x, int y) {
int rootx = parent[x];
int rooty = parent[y];
if (rootx != rooty) {
if (rank[rootx] > rank[rooty]) {
parent[rooty] = rootx;
} else if (rank[rooty] >= rank[rootx]) {
parent[rootx] = rooty;
}
} else {
parent[rooty] = rootx;
rank[rooty] += 1;
}
}

public static boolean validStatements(char[][] equal, char[][] unequal) {

for (char[] pair : equal) {
union(pair[0] - 'A' + 1, pair[1] - 'A' + 1);
// System.out.println((pair[0] - 'A') + " , " + (pair[1] - 'A'));
}

for (char[] opp : unequal) {
if (getParent(opp[0] - 'A' + 1) == getParent(opp[1] - 'A' + 1))
return false;
}

return true;
}

public static void main(String[] args) {
char[][] equal = { { 'A', 'B' }, { 'B', 'D' }, { 'C', 'D' }, { 'F', 'G' }, { 'E', 'H' }, { 'H', 'C' } };
char[][] unequal = { { 'A', 'N' } };

boolean res = validStatements(equal, unequal);
System.out.println(res);
}

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

``````public static boolean validStatements(String[][] equal, String[][] unequal) {
HashMap<String,Integer> hm = new HashMap<String,Integer>();
for(String[] pair: equal) {
hm.put(pair[0]+pair[1],1);
hm.put(pair[1]+pair[0],1);
}
for(String[] pair: unequal) {
if (hm.containsKey(pair[1]+pair[0]) || hm.containsKey(pair[0]+pair[1])) {
return false;
}
}
return true;
}``````

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.

### 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.