Shutterfly Interview Question for Software Engineer / Developers


Country: United States




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

private Node mirror(Node root) {
    if(root==null) {
        return null;
    } else {
        Node new_root = new Node();
        new_root.data = root.data;
        new_root.leftChild = mirror(root.rightChild);
        new_root.rightChild = mirror(root.leftChild);

        return new_root;
    } 
}

- tazo August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

To make this thread-safe, we can simply make the function synchronized, right? Here's my solution:

public synchronized void mirror(Node root) {
    if (root == null) {
        return;
    } else {
        mirror(root.left);
        mirror(root.right);

        Node temp = root.right;
        root.right = root.left;
        root.left = temp;
    }
}

Now, it is possible to split the tree into sub-trees to improve thread-safety but this is simple and should just work.

- Anonymous August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I couldn't get the second part, so the interviewer asked a follow-up question to make my mirror() solution thread-safe.

- edoc0code August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mirror(struct treenode* root)
{
struct treenode* temp=NULL;
if(root==NULL)
return;

mirror(root->left);
mirror(root->right);

temp=root->left;
root->left=root->right;
root->right=temp;
}

- atul gupta August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To get around using a temp variable in your mirror function you can pass a variable by ref. Here is a sample that might work

void mirrorTree(tree_node * root, tree_node ** n)
{
    if(!root)
    {
        *n = NULL;
        return;
    }

    *n = new tree_node;
    (*n)->data = root->data;

    mirrorTree(root->left,&(*n)->left);
    mirrorTree(root->right, &(*n)->right);
}

- Tom Code Man August 08, 2012 | 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