Amazon Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
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 A = B add map.Add('A','A'); map.Add('B', 'A');
if(!map.ContainsKey(seta[i, 0]) && !map.ContainsKey(seta[i,1])){
map.Add(seta[i,0], seta[i,0]);
map.Add(seta[i,1], seta[i,0]);
}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]];
map.Add(key,value);
}else{ //if D = C, then map.Add('D', 'A');
var key = seta[i,0];
var value = map[seta[i,1]];
map.Add(key,value);
}
}
//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;
}
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);
}
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;
}
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.
Looking for interview questions sharing and mentors? Visit A++ Coding Bootcamp at aonecode.com (select english at the top right corner).
- aonecoding4 December 25, 2018We 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.