Amazon Interview Question for SDE-3s


Country: India
Interview Type: In-Person




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

boolean isBloodRelated(int id1, int d2){
	if(id1 == id2) return true;
	HashSet<Integer> idSet = new HashSet<>();
	Deque<Integer> queue = new ArrayDeque<>();
	idSet.add(id1);
	idSet.add(id2);
	queue.add(id1);
	queue.add(id2);
	while(!queue.isEmpty()){
		int id = queue.poll();
		Card card = getCard(id);
		int momId = card.getMomId();
		int dadId = card.getDadId();
		if(idSet.contains(momId) || idSet.contains(dadId))return true;
		if(momId != -1){
			queue.add(momId);
			idSet.add(momId);
		}
		if(dadId != -1){
			queue.add(dadId);
			idSet.add(dadId);
		}
	}
	return false;
	}

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

Most important thing is data's organization. Because card objects are billions. But I assumed CardManager class already made this system.

bool isBloodRelated(std::string& id1, std::string& id2) {
    if (id1 == id2) {
        return true;
    }

    JsonObject id1Card;
    if (!CardManager::getCard(id1, id1Card)) {
        throw invalid_argument("Invalid PersonalId: " + id1);
    }

    JsonObject id2Card;
    if (!CardManager::getCard(id1, id2Card)) {
        throw invalid_argument("Invalid PersonalId: " + id2);
    }

    std::set<std::string> blooadRelatedKeys = CardManager::getBloodRelatedKeys();
    if (isMember(id1Card, blooadRelatedKeys)) {
        return true;
    }

    if (isMember(id2Card, blooadRelatedKeys)) {
        return true;
    }

    return false;
}

bool isMember(JsonObject& card, std::set<std::string>& checkingKeys) {
    bool isMember = false;

    std::set<std::string>::iterator iter;
    for (iter = checkingKeys.begin(); iter != checkingKeys.end(); ++iter) {
        std::string& familyId = card.get(*iter);
        if (familyId == id) {
            isMember = true;
            break;
        }
    }

    return isMember;
}

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

Assuming cards is a collection of all the objects

void isBloodRelated(String id1, String id2){
        Map<String, Card> cardId2Obj =  cards.parallelStream().collect( Collectors.toMap( card-> card.myId, card-> card ));
        Card card1 = cardId2Obj.get( id1 );
        Card card2 = cardId2Obj.get( id2 );
        boolean relation = false;
        if(card1.myId.equals( card2.myId )){
            relation = true;
        }else if(card1.momId.equals( card2.myId ) || card1.myId.equals( card2.momId )){
            relation = true;
        }else if(card1.dadId.equals( card2.myId ) || card1.myId.equals( card2.dadId )){
            relation = true;
        }else if(card1.dadId.equals( card2.dadId ) || card1.momId.equals( card2.momId )){
            relation = true;
        }
        else{
            relation = false;
        }

    }

- Chaitanya Kumar August 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Basic check

public boolean checkBloodRelation(Person p1, Person p2)
{
	if(p1 == null || p2 == null)
		return false
	if(this.isBloodRelated(p1,p2)) return true;
	
	return(checkBloodRelation(p1.momid, p2.momid)||checkBloodRelation(p1.momid, p2.dadid)||checkBloodRelation(p1.dadid, p2.momid)||checkBloodRelation(p1.dadid, p2.dadid)); 
}
public boolean isBloodRelated(Person p1, Person p2)
{

if (p1==p2 || p1.momid==p2.momid || p1.dadid == p2.dadid)
	return true;
else
	return false;
}

- anshuman101 February 12, 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