Amazon Interview Question for Software Developers


Country: India
Interview Type: Written Test




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

import java.util.*;

class Node {
    int val;
    Node left,right,random;

    Node(int x) {
        val = x;
    }
}

class InorderStorePath {
    public static void main(String args[]) {
        Node x = new Node(4);
        x.left = new Node(2);
        x.right = new Node(6);
        x.left.left = new Node(1);
        x.left.right = new Node(3);
        x.right.left = new Node(5);
        x.right.right = new Node(7);
        inorderConstruct(x);
        while(x.left!=null) {
            x = x.left;
        }
        while(x!=null) {
            System.out.print(x.val+" ");
            x = x.random;
        }
    }

    public static void inorderConstruct(Node root) {
        Stack<Node> s = new Stack<Node>();
        Node current = root;
        Node prev = null;
        while(true) {
            if(current!=null) {
                s.push(current);
                current = current.left;
            }
            else if(!s.isEmpty()) {
                current = s.pop();
                if(prev!=null) {
                    prev.random = current;
                }
                prev = current;
                current = current.right;
            }
            else {
                break;
            }
        }
    }
}

- dklol February 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I don't understand the question. Can you give an example please?

- Asif Garhi February 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class BinarySearch {
    public static void main(String args[]) {
        int x[]= {0,1,2,3,4,5,6,7,8};
        for(int i=0;i<=13;i++) {
            System.out.println("Position of "+i+" : "+BS(x,0,x.length-1,i)+".");
        }
    }

    public static int BS(int x[],int low,int high,int key) {
        if(high<low) {
            return -1;
        }
        else {
            int mid = low + (high-low)/2;
            if(x[mid]==key) {
                return mid;
            }
            else if(key>x[mid]) {
                return BS(x,mid+1,high,key);
            }
            else {
                return BS(x,low,mid-1,key);
            }
        }
    }
}

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

#include <iostream>
using namespace std;

/* A binary tree node has data, pointer to left child, a pointer to right
child and a pointer to random node*/
struct Node
{
int key;
struct Node* left, *right, *random;
};

/* Helper function that allocates a new Node with the
given data and NULL left, right and random pointers. */
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->random = temp->right = temp->left = NULL;
return (temp);
}

/* Given a binary tree, print its Nodes in inorder*/
void printInorder(Node* node)
{
if (node == NULL)
return;

/* First recur on left sutree */
printInorder(node->left);

/* then print data of Node and its random */
cout << "[" << node->key << " ";
if (node->random == NULL)
cout << "NULL], ";
else
cout << node->random->key << "], ";

/* now recur on right subtree */
printInorder(node->right);
}

/* Driver program to test above functions*/
int main()
{

Node *tree = newNode(1);
tree->left = newNode(2);
tree->right = newNode(3);
tree->left->left = newNode(4);
tree->left->right = newNode(5);
tree->random = tree->left->right;
tree->left->left->random = tree;
tree->left->right->random = tree->right;


cout << "Inorder traversal of original binary tree is: \n";
printInorder(tree);

return 0;
}

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

public static void inorder(Node root) {
	inorderHelper(null,root);
}

private static void inoderHelper(Node randomIter, Node iter) {
	if(iter == null) 
		return;
	inorderHelper(randomIter,iter.left);
	
	if(randomIter == null)
		randomIter = iter;
	else {
		randomIter.random = iter;
		randomIter = iter;
	}
	
	inorderHelper(randomIter,iter.right);
}

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

class TreeNode:
    def __init__(self, x, l=None, r=None):
        self.val = x
        self.left = l
        self.right = r
        self.in_next=None

def rec_in(t, t_next):
    if t:
        t.in_next = rec_in(t.right, t_next)
        return rec_in(t.left, t)
    return t_next

- hrushikesh.iitb February 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please let me know the if it will work for non empty tree.

inorderPath(root, root->left);
/*for non empty tree, has at least one node*/
tree* inorderPath(tree *root, tree* next){
	if (root == NULL || next == NULL) return root;
	next->random = inorderPath(root->left, next->left);
	inorderPath(root->right, root);
	return root;
}

- praveen pandit March 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

inorderPath(root,NULL,1);

void inorderPath(tree *root, tree* prev, int flag){
	if (root == NULL) return;
	inorderPath(root->left, root, 1);
	if (flag) root->random = prev;
	else prev->random = root;	
	inorderPath(root->right, root, 0);
}

- praveen pandit March 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c# implementation, NO recursion, keep a reference of the previous in order node to set up its Random property.

public class RandomTreeNode
{
    public int Value;
    public RandomTreeNode Left;
    public RandomTreeNode Right;
    public RandomTreeNode Random;
}

public RandomTreeNode SetInorderTraversal(RandomTreeNode root)
{
    if (root == null)
        return null;

    var prev = new RandomTreeNode();
    var first = prev;
    var stack = new Stack<RandomTreeNode>();
    var node = root;

    while (node != null && stack.Count > 0)
    {
        if (node == null)
        {
            node = stack.Pop();
            prev.Random = node;
            prev = node;

            node = node.Right;
            break;
        }

        stack.Push(node);
        node = node.Left;
    }

    return first.Random;
}

- hnatsu March 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <conio.h>

using namespace std;

struct Tree
{
	int data;
	Tree* left, *right, *random;
	Tree(int d)
	{
		data = d;
		left = right = random = NULL;
	}
};

void inorder(Tree* root)
{
	if(!root)
		return;

	inorder(root->left);
	cout<<root->data<<" ";
	inorder(root->right);
}

void inorderWithRandom(Tree* root)
{
	if(!root)
		return;

	Tree* curr, *pre;

	curr = root;
	while(curr)
	{
		if(curr->left == NULL)
		{
			cout<<curr->data<<" ";
			curr = curr->right ? curr->right : curr->random;
		}
		else
		{
			pre = curr->left;
			while(pre->right && !pre->random)
			{
				pre = pre->right;
			}

			if(!pre->right && !pre->random)
			{
				pre->random = curr;
				curr = curr->left;
			}
			else
			{
				cout<<curr->data<<" ";
				curr = curr->right ? curr->right : curr->random;
			}
		}
	}
}

void main()
{
	Tree* root = new Tree(6);
	root->left = new Tree(3);
	root->right = new Tree(10);
	root->left->left = new Tree(1);
	root->left->right = new Tree(5);
	root->right->left = new Tree(7);
	root->left->left->right = new Tree(2);
	root->left->right->left = new Tree(4);
	root->right->left->right = new Tree(8);
	root->right->left->right->right = new Tree(9);

	//inorder(root);
	inorderWithRandom(root);

	getch();
}

- peaceLover April 10, 2016 | 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