Apple Interview Question for SDE1s


Country: United States




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

public class Node<E extends Comparable<? super E>> implements Iterable<E>
{
	private Node<E> left;
	private Node<E> right;
	private E value;

	public Node(E value)
	{
		this.value = value;
	}

	public Node<E> left(E leftval)
	{
		left = new Node<>(leftval);
		return left;
	}

	public Node<E> right(E rightval)
	{
		right = new Node<>(rightval);
		return right;
	}

	public Iterator<E> iterator()
	{
		return new BstIterator(this);
	}

	private class BstIterator implements Iterator<E>
	{
		LinkedList<Node<E>> stack;
		BstIterator(Node<E> root)
		{
			stack = new LinkedList<>();
			Node<E> node = root;
			while (node != null)
			{
				stack.push(node);
				node = node.left;
			}
		}

		@Override
		public void remove()
		{
			throw new UnsupportedOperationException();
		}

		@Override
		public boolean hasNext()
		{
			return !stack.isEmpty();
		}

		@Override
		public E next()
		{
			if (!hasNext())
				throw new NoSuchElementException();

			Node<E> node = stack.pop();
			E retval = node.value;

			node = node.right;
			while (node != null)
			{
				stack.push(node);
				node = node.left;
			}

			return retval;
		}
	}
	
	public static void main(String... args)
	{
		Node<Integer> ten = new Node<>(10),
		five = ten.left(5),
		four = five.left(4),
		seven = five.right(7),
		six = seven.left(6),
		three = four.left(3),
		one = three.left(1),
		two = one.right(2),
		twenty = ten.right(20),
		fifteen = twenty.left(15),
		seventeen = fifteen.right(17),
		thirty = twenty.right(30);
		
		for (Integer num : ten)
		{
			System.out.println(num);
		}
	}
}

- jakob February 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Doing an inorder traversal will iterate through the nodes in ascending order, but the iterating must be done in steps, which makes it so you can't use a standard recursive inorder DFS traversal. So instead find the leftmost child of the current node and maintain a stack of "found but not processed" nodes. For the next node, pop the next node off the stack, then find the leftmost child of its right child (since you would have already processed its left child), again putting all encountered nodes on the stack.

- c February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

please clarify what you want to say

- shashank February 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

class node{
public:
	int key;
	node* left;
	node* right;
	node(int key) {
		this->key = key;
		left = right = 0;
	}
};

class Tree{
	node* root;

public:
	Tree(node *p):root(p) {}

	void addNode(int key) {
		node *p = new node(key);
		addNode(p);
	}
	
	void addNode(node *p) {
		node* curr = root;
		node *prev = 0;
		while(curr) {
			if (curr->key == p->key) {
				cout<<"Duplicate key "<<p->key<<endl;
				return;
			}
			prev = curr;
			if (curr->key < p->key) {
				curr = curr->right;
			} else {
				curr = curr->left;
			}
		}
		
		if (prev && prev->key < p->key) prev->right = p; else prev->left = p;
	}
	
	node* find(int key) {
		node* curr = root;
		while(curr) {
			if (curr->key == key) {
				return curr;
			}
			if (curr->key < key) {
				curr = curr->right;
			} else {
				curr = curr->left;
			}
		}
		
		return NULL;
	}
	
	~Tree() {
		deleteTree(root);
	}
	
	void deleteTree(node *p) {	//Deletion using Post Order traversal
		if (!p) return;
		deleteTree(p->left);
		deleteTree(p->right);
//		cout<<"Deleting "<<p->key<<endl;
		delete p;
	}
	
	class iterator{
		Tree *t;
		node *itrPtr;
		
	public:
		iterator(Tree *t, node *p):t(t),itrPtr(p) {}

		void operator ++(int) {
			if (itrPtr->right) {//Go one right, then left all the way to bottom
				itrPtr = itrPtr->right;
				for(;itrPtr->left;itrPtr=itrPtr->left);
			} else {//successor will be the "nearest" ancestor to itrPtr such that itrPtr is in the left subtree of successor
				node *curr = t->root,*successor = 0;
				while(curr) {
					if (curr->key == itrPtr->key) {
						itrPtr = successor;
						return;
					}
					if (curr->key < itrPtr->key) {
						curr = curr->right;
					} else {
						successor = curr;
						curr = curr->left;
					}
				}
			}
		}
		
		node *operator *() {
			return itrPtr;
		}

		bool operator !=(iterator itr) {
			if (itrPtr != itr.itrPtr) return true; else return false;
		}
	};

	Tree::iterator begin() {
		node *p;
		for(p=root;p->left;p=p->left);
		return Tree::iterator(this,p);
	}

	Tree::iterator end() {
		return Tree::iterator(this,NULL);
	}
};


int main() {
	Tree t(new node(100));
	t.addNode(50);
	t.addNode(25);
	t.addNode(10);
	t.addNode(40);
	t.addNode(30);
	t.addNode(45);
	t.addNode(75);
	t.addNode(65);
	t.addNode(90);
	t.addNode(95);
	t.addNode(80);
	t.addNode(77);
	t.addNode(150);
	t.addNode(130);
	t.addNode(110);
	t.addNode(120);
	t.addNode(170);
	t.addNode(160);
	
	for(Tree::iterator itr = t.begin();itr != t.end();itr++) {
		static int ctr=0;
		ctr++;
		cout<<"node-"<<ctr<<": "<<(*itr)->key<<endl;
	}
}

- Ganesh Ram Sundaram February 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

just do inorder traversal of bst and it will provide you the nodes in ascending order

- Anonymous February 01, 2014 | 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