Amazon Interview Question for Software Engineer / Developers






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

it can be done in O(n) and O(1) space by manipulating the left/right child pointers.
Below is pseudo code:
*** I haven't compiled this.

void DeleteTree (node*  root)
{
	node* tempRoot = root;
	node* tempLeft = root->left;
	node* tempRight = root->right;
	node* tempNode = NULL;

	while (tempLeft || tempRight)
	{
		if (tempLeft)
		{
			tempNode = tempLeft->left;
			tempLeft->left = tempRoot;
			tempRoot =  tempLeft;
			tempLeft = tempNode;
			continue; /*this is important*/
		}
		tempRight = tempRoot->right;

		if (tempLeft == NULL && tempRight == NULL)
		{
			tempNode = tempRoot->left;
			delete (tempRoot);
			tempRoot = NULL;
			tempRoot = tempNode;
			tempLeft = tempRoot->right;
		}

		else
		{
			tempNode = tempRight->left;
			tempRight->left = tempRoot;
			tempLeft = tempNode;
			tempRoot = tempRight;
		}
	}
	delete (tempRoot);
}

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

1. Convert BST to single Pointer LL. --- O(N)
2. Start delete node from LL one after another. --- O(N)

Total Complexity: O(2N)

Is the above solution Aceptable ?

- SRB February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void DeleteBSTree (Node *Root)
{
	Node *Temp, *TRight;
	Temp = Roor;
	
	while(Temp)// Go to the Right most node
	{
		TRight = Temp;
		Temp = Temp->Right;
	}
	
	while(Root)
	{
		if(Root->Left) //Convert to LL
			TRight->Right = Root->Left;

		DeleteNode = Root;
		Root = Root->Right;
		Free(DeleteNode)
	}
}

- SRB February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node temp=root,trash;
while(root){
  while(root.right!=null)
    temp=root.right;
  if(root==temp){
    trash=root;
    root=root.left;
    temp=root;
    free(trash);
    continue;
  }
  temp.right=root.left;
  trash=root;
  root=root.right;  
  free(trash)
}

- Sathya February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice Solution :)

- krishna June 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct me if i am wrong, but in languages like java, an object that is not referred to by any other object is deleted, so root.finalize() should be enough rest all will be taken care by the java garbage collector. Any views?

- swap1712 February 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't read the "memory must be handled manually" requirement at first, ignore the previous comment.

- swap1712 February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think that deleting the nodes as they are encountered in a breadth first search(BFS) would work. Depth first search cannot be used because then we cannot delete nodes as we encounter them but for BFS this is possible. The time complexity would be O(n) since each node is visited exactly once and deleted.

- aragorn February 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will below solution work.
First delete Left subtree then right subtree. Then free the Node.
I guess complexity will be O(n) only.

delect (Node root)
{
if(root->left !=null)
delete(root->left);
if(root->right != null)
delete(root->right);
free(root)
}

- Piyush February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Read the question.It says recursion is not allowed

- Arijit April 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't it be solved just by a postorder tree walk?

- Rip February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am also thinking same because if we delete root first then we loss the access to others..but if we do postorder then we can delete tree easily.It will use system stack and recursion.

- ashish February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you want to do postorder without recursion then we can do it by using stack and flag.

- ashish February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Post-order is what is usually done, but the conditions of the problem prevented this. No recursion is specifically stated, as is O(1) memory so no stack is possible.

- Anonymous March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Maintain 2 stacks one for the parent and other for the sibling.... keep throwing the root and the sibling in the stacks(maintain some info if the sibling is present or not) if the node is not left with any children remove it and move ot its sibling in the other stack.

Do this in a while loop and the siblings will move into the main root stack.

- ali February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ali: space is O(n) in your case.

- Tulley February 17, 2011 | Flag


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