Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

public boolean areEqual(Node node1, Node node2) {
    if (node1 == null && node2 == null) {
		return true;
	} 
	if (node1 == null || node2 == null || node1.value != node2.value) {
		return false;
	}
	 
	if ((areEqual(node1.left, node2.left) && areEqual(node1.right, node2.right)) ||  (areEqual(node1.left, node2.right) && areEqual(node1.right, node2.left))) {
		return true;
	}	 
	return false;
 }

- vc January 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Given two trees, return true if they are
 structurally identical */
int identicalTrees(struct node* a, struct node* b)
{
    /*1. both empty */
    if (a==NULL && b==NULL)
        return 1;
 
    /* 2. both non-empty -> compare them */
    if (a!=NULL && b!=NULL)
    {
        return
        (
            a->data == b->data &&
            identicalTrees(a->left, b->left) &&
            identicalTrees(a->right, b->right)
        );
    }
     
    /* 3. one empty, one not -> false */
    return 0;
}

- NIT January 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool compare(TreeNode* a, TreeNode* b)
{ 
    if (a == NULL && b == NULL) return true;
    else if (a == NULL ^ b == NULL) return false;
    bool noswap = compare(a->left, b->left) && compare(a->right, b->right);
    bool swap = compare(a->left, b->right) && compare(a->right, b->left);
    return a->val == b->val && (swap | noswap); 
}

- Anonymous January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem, also referred as Tree Isomorphism can be implemented recursively. The approach is to implement the code that detects if two trees are identical and add component that considers thatc branches have been swapped. Below is C/C++ code

bool isomorphic(node *n1, node *n2)
{
	if (n1==NULL && n2==NULL) return true;
	if (n1==NULL || n2==NULL) return false;
	if (n1->val != n2->val) return false;
			 //have not been swapped
	return (isomorphic(n1->left, n2->left) && isomorphic(n1->right && n2->right) ||
			isomorphic(n1->left, n2->right) && isomorphic(n1->right, n2->left));
			//have been swapped
}

- alex January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Your solutions work in O(2 ^ n) time, add memorization to make complexity O(n ^ 2)

- Askhat February 05, 2014 | 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