xyz Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

#include<stdio.h>
#include <iostream>

using namespace std;

static int marker = 0;

class Node {

public:
    int val;
    int mark;
    Node * p;
    Node * leftchild;
    Node * rightchild;

    Node(int value, Node * parent){
        val = value;
        p = parent;
        mark = 0;
        leftchild = nullptr;
        rightchild = nullptr;
    }
    
    void insterL(int val){
        leftchild = new Node(val, this);
    }
    
    void insterR(int val){
        rightchild = new Node(val, this);;
    }
    
};

void printTree(Node * node){
    if (node == nullptr) {
        return;
    }
    
    printTree(node->leftchild);
    marker++;
    node->mark = marker;
    cout << "node:" <<node->val << "  #"<< node->mark <<endl;
    printTree(node->rightchild);
    
}

void reverseTree(Node * node){
    if (node == nullptr) {
        return;
    }
    
    reverseTree(node->leftchild);
//    cout << "node:" <<node->val <<endl;
    
    if (node->p == nullptr) {
        node->rightchild = nullptr;
    }else{
        node->rightchild = node->p->rightchild;
    }
    
    node->leftchild = node->p;
    node->p = node->leftchild;

}

Node * getReverseTree(Node * node){
    
    Node * temp = node;
    while(temp->leftchild != nullptr){
        temp = temp->leftchild;
    }
    
    
    reverseTree(node);
    
    return temp;
}

int main()
{
    Node * root = new Node(1, nullptr);
    root->insterL(2);
    root->insterR(3);
    
    root->leftchild-> insterL(4);
    root->leftchild-> insterR(5);
    
    root->leftchild-> leftchild-> insterL(6);
    root->leftchild-> leftchild-> insterR(7);
    
    root->rightchild->insterR(-1);
    
    
    printTree(getReverseTree(root));

    return 0;
}

- albertchenyu February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Shouldnt it be zig-zig or Zig-Zag for rotations till the node you want to make the parent?

- Lazad February 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is a simple java code:

ReverseLeftNodeOfBT(Node node)
{
if (node==null)
return;

Node temp=node.right;
ReverseLeftNodeOfBT(node.left);
node.left.right=temp;
node.right=null;

}

- Anonymous February 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am assuming it will be like convert:

from:
		1
		/\
	       2 3
	      / \
            4   5
           / \
         6   7
        /  \
      8    9

To:
		8
		/\
	       6 9
	      / \
            4   7
           / \
         2   5
        /  \
      1    3

Java code:

public static void ReverseLeftNode(Node node)
{
	if(node.left == null)
	{
		head = node;
		return;
	}
	
	Node temp = node.right;
	ReverseLeftNode(node.left);
	node.left.right = temp;
	node.left.left = node;
}

note: some modification in one of the answers above.

- chetan February 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

What do you mean by 6 is root? If you keep same root (1) my understanding is you need to convert

1
	   /        \
          2            3
        /   \
      4       5
     /   \
   6     7           

to 
1
|
2 -- 3
|
4  -- 5
|
6 -- 7

- Prashant February 22, 2015 | 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