Linkedin Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

starting from root recursively swap left child and right child at all the nodes.

- zr.roman January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mirrorTree(TreeNode* root){
if(root == NULL)
return;
mirrorTree(root->left);
mirrorTree(root->right);
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
}

- zen January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive Algorithm:

swape_sub_tree(node){
left_tree_node = node->left node;
right_tree_node = node->right node;
node->left node = right_tree_node;
node->right node = left_tree_node;
swape_sub_tree(left_tree_node);
swape_sub_tree(right_tree_node);
}

- MEET BRAHMBHATT January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

swape_subtree(node){
	left_tree_node = node->left_node;
	right_tree_node = node->right_node;
	node->left_node = right_tree_node;
	node->right_node = left_tree_node;
	swape_subtree(right_tree_node);
	swape_subtree(left_tree_node);
}

- Meet Brahmbhatt January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{

void mirror(node * n)
{
	node * temp;
	if (n != NULL)
	{
		mirror(n->left);
		mirror(n->right);
		temp = n->left;
		n->left = n->right;
		n->right = temp;


	}



}

}

- Engineer1111 January 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Node {
    public Node left = null;
    public Node right = null;

    public Node(Node left, Node right) {
        this.left = left;
        this.right = right;
    }
}

public Node reverse(Node x) {
    if (x == null) {
        return null;
    }
    Node temp = x.left;
    x.left = reverse(x.right);
    x.right = reverse(temp);
    return x;
}

- davide.marino February 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AlgorithmsTests
{
    [TestClass]
    public class ReverseATree
    {
        [TestMethod]
        public void Reverse_Test()
        {
            /*       1
                    / \
                   2   4
                  /     \
                 3       5
            */
            Node head = new Node()
            {
                Value = 1,
                Left = new Node()
                {
                    Value = 2,
                    Left = new Node()
                    {
                        Value = 3
                    }
                },
                Right = new Node()
                {
                    Value = 4,
                    Right = new Node()
                    {
                        Value = 5
                    }
                }
            };

            Reverse(head);

            Assert.AreEqual(1, head.Value);
            Assert.AreEqual(4, head.Left.Value);
            Assert.AreEqual(2, head.Right.Value);
            Assert.AreEqual(5, head.Left.Left.Value);
            Assert.AreEqual(3, head.Right.Right.Value);
        }

        private void Reverse(Node n)
        {
            if (n == null)
            {
                return;
            }

            Node tmp = n.Left;
            n.Left = n.Right;
            n.Right = tmp;

            Reverse(n.Left);
            Reverse(n.Right);
        }
    }

    class Node
    {
        public Object Value;
        public Node Left;
        public Node Right;
    }
}

- Solution in C#, with tests February 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AlgorithmsTests
{
    [TestClass]
    public class ReverseATree
    {
        [TestMethod]
        public void Reverse_Test()
        {
            /*       1
                    / \
                   2   4
                  /     \
                 3       5
            */
            Node head = new Node()
            {
                Value = 1,
                Left = new Node()
                {
                    Value = 2,
                    Left = new Node()
                    {
                        Value = 3
                    }
                },
                Right = new Node()
                {
                    Value = 4,
                    Right = new Node()
                    {
                        Value = 5
                    }
                }
            };

            Reverse(head);

            Assert.AreEqual(1, head.Value);
            Assert.AreEqual(4, head.Left.Value);
            Assert.AreEqual(2, head.Right.Value);
            Assert.AreEqual(5, head.Left.Left.Value);
            Assert.AreEqual(3, head.Right.Right.Value);
        }

        private void Reverse(Node n)
        {
            if (n == null)
            {
                return;
            }

            Node tmp = n.Left;
            n.Left = n.Right;
            n.Right = tmp;

            Reverse(n.Left);
            Reverse(n.Right);
        }
    }

    class Node
    {
        public Object Value;
        public Node Left;
        public Node Right;
    }
}

- Solution in C#, with tests February 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AlgorithmsTests
{
    [TestClass]
    public class ReverseATree
    {
        [TestMethod]
        public void Reverse_Test()
        {
            /*       1
                    / \
                   2   4
                  /     \
                 3       5
            */
            Node head = new Node()
            {
                Value = 1,
                Left = new Node()
                {
                    Value = 2,
                    Left = new Node()
                    {
                        Value = 3
                    }
                },
                Right = new Node()
                {
                    Value = 4,
                    Right = new Node()
                    {
                        Value = 5
                    }
                }
            };

            Reverse(head);

            Assert.AreEqual(1, head.Value);
            Assert.AreEqual(4, head.Left.Value);
            Assert.AreEqual(2, head.Right.Value);
            Assert.AreEqual(5, head.Left.Left.Value);
            Assert.AreEqual(3, head.Right.Right.Value);
        }

        private void Reverse(Node n)
        {
            if (n == null)
            {
                return;
            }

            Node tmp = n.Left;
            n.Left = n.Right;
            n.Right = tmp;

            Reverse(n.Left);
            Reverse(n.Right);
        }
    }

    class Node
    {
        public Object Value;
        public Node Left;
        public Node Right;
    }
}

- Anonymous February 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AlgorithmsTests
{
    [TestClass]
    public class ReverseATree
    {
        [TestMethod]
        public void Reverse_Test()
        {
            /*       1
                    / \
                   2   4
                  /     \
                 3       5
            */
            Node head = new Node()
            {
                Value = 1,
                Left = new Node()
                {
                    Value = 2,
                    Left = new Node()
                    {
                        Value = 3
                    }
                },
                Right = new Node()
                {
                    Value = 4,
                    Right = new Node()
                    {
                        Value = 5
                    }
                }
            };

            Reverse(head);

            Assert.AreEqual(1, head.Value);
            Assert.AreEqual(4, head.Left.Value);
            Assert.AreEqual(2, head.Right.Value);
            Assert.AreEqual(5, head.Left.Left.Value);
            Assert.AreEqual(3, head.Right.Right.Value);
        }

        private void Reverse(Node n)
        {
            if (n == null)
            {
                return;
            }

            Node tmp = n.Left;
            n.Left = n.Right;
            n.Right = tmp;

            Reverse(n.Left);
            Reverse(n.Right);
        }
    }

    class Node
    {
        public Object Value;
        public Node Left;
        public Node Right;
    }
}

- Octavio Licea February 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// This one returns a mirrored copy of the original.
Node* GetMirrorTree(Node *node)
{
	if (node == nullptr)
	{
		return nullptr;
	}
	
	Node *newNode = new Node;
	newNode->data = node->data;
	newNode->left = GetMirrorTree(node->right);
	newNode->right = GetMirrorTree(node->left);
	
	return newNode;
}

- Gyan Garcia April 09, 2017 | 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