Ebay Interview Question for Software Engineer / Developers






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

node *find(node *root, int val) {
if (!root) return NULL;

if (root->val == val) return root;

if (val < root->val) return find(root->left);
else return find(root->right);
}

- nunbit romance June 05, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The provided solution is for binary search tree. But the question is about binary tree..

- alien April 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

He is asking for finding the element and deleting it as well. Not just finding the element.

- mac August 02, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@nunbit Is the question for a binary tree or binary search tree?

- Musheka January 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node find(int num Node root)
{
if(rooyt == null)
return null

if(root.value== num)
return node

else
{
if(root.leftChild != null)
return find(root.leftChild)

if(root.rightChild != null)
return find(root.rightChild)

}


}

- anshulzunke September 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Void delete(key value, Node root){
Node toBeDeleted = find(value,root);

if(toBeDeleted.rightChild == Null && toBeDeleted.leftChild == Null){
if(toBeDeleted.parent.RightChild.value == toBeDeleted.value) toBeDeleted.parent.RightChild = Null;
if(toBeDeleted.parent.LeftChild.value == toBeDeleted.value) toBeDeleted.parent.LeftChild = Null;
}

else if(toBeDeleted.rightChild == Null && toBeDeleted.leftChild != Null){
toBeDeleted.leftChild.parent = toBeDeleted.parent;
if(toBeDeleted.parent.RightChild.value == toBeDeleted.value) toBeDeleted.parent.RightChild = Null;
if(toBeDeleted.parent.LeftChild.value == toBeDeleted.value) toBeDeleted.parent.LeftChild = Null;
}
else if(toBeDeleted.rightChild != Null && toBeDeleted.leftChild == Null){
toBeDeleted.RightChild.parent = toBeDeleted.parent;
if(toBeDeleted.parent.RightChild.value == toBeDeleted.value) toBeDeleted.parent.RightChild = Null;
if(toBeDeleted.parent.LeftChild.value == toBeDeleted.value) toBeDeleted.parent.LeftChild = Null;
}

else{
Node newNode = find_Successor(toBedDeleted);
delete(newNode);
toBeDeletedNode.value = newNode.value;
}
}

use the find method given above by anshulzuke

- praveens@iitrpr.ac.in December 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryTree {

	Node root;

	public void addNode(int key, String name) {
		Node newNode = new Node(key, name);
		if (root == null) {
			root = newNode;
		} else {
			Node focusNode = root;
			Node parent;
			while (true) {
				parent = focusNode;
				if (key < focusNode.key) {
					focusNode = focusNode.leftChild;
					if (focusNode == null) {
						parent.leftChild = newNode;
						return;
					}
				} else {
					focusNode = focusNode.rightChild;
					if (focusNode == null) {
						parent.rightChild = newNode;
						return;
					}
				}
			}
		}
	}


	public boolean remove(int key) {
		Node focusNode = root;
		Node parent = root;

		boolean isItALeftChild = true;
		while (focusNode.key != key) {
			parent = focusNode;
			if (key < focusNode.key) {
				isItALeftChild = true;
				focusNode = focusNode.leftChild;
			} else {
				isItALeftChild = false;
				focusNode = focusNode.rightChild;
			}
			if (focusNode == null) {
				return false;
			}
		}
		if (focusNode.leftChild == null && focusNode.rightChild == null) {
			if (focusNode == root) {
				root = null;
			} else if (isItALeftChild) {
				parent.leftChild = null;
			} else {
				parent.rightChild = null;
			}
		} else if (focusNode.rightChild == null) {
			if (focusNode == root) {
				root = focusNode.leftChild;
			} else if (isItALeftChild) {
				parent.leftChild = focusNode.leftChild;
			} else {
				parent.rightChild = focusNode.leftChild;
			}
		} else if (focusNode.leftChild == null) {
			if (focusNode == root) {
				root = focusNode.rightChild;
			} else if (isItALeftChild) {
				parent.leftChild = focusNode.rightChild;
			} else {
				parent.rightChild = focusNode.leftChild;
			}
		} else {
			Node replacement = getReplacementNode(focusNode);
			if (focusNode == root) {
				root = replacement;
			} else if (isItALeftChild) {
				parent.leftChild = replacement;
			} else {
				parent.rightChild = replacement;
			}
			replacement.leftChild = focusNode.leftChild;
		}
		return true;
	}

	public Node getReplacementNode(Node replacedNode) {
		Node replacementParent = replacedNode;
		Node replacement = replacedNode;
		Node focusNode = replacedNode.rightChild;
		while (focusNode != null) {
			replacementParent = replacement;
			replacement = focusNode;
			focusNode = focusNode.leftChild;
		}

		if (replacement != replacedNode.rightChild) {
			replacementParent.leftChild = replacement.rightChild;
			replacement.rightChild = replacedNode.rightChild;
		}
		return replacement;
	}

	public static void main(String[] args) {

		BinaryTree theTree = new BinaryTree();
		theTree.addNode(50, "Boss");
		theTree.addNode(25, "Vice President");
		theTree.addNode(15, "Office Manager");
		theTree.addNode(30, "Secretary");
		theTree.addNode(75, "Sales Manager");
		theTree.addNode(85, "Salesman 1");
		System.out.println("\nRemove key 25");
		theTree.remove(25);


	}
}

class Node {
	int key;
	String name;
	Node leftChild;
	Node rightChild;

	Node(int key, String name) {
		this.key = key;
		this.name = name;
	}

	public String toString() {
		return name + " has the key " + key;
	}
}

- adijhu June 12, 2013 | 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