Amazon Interview Question for SDE1s


Country: United States




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

trie datastructure

- pm June 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

my bad....its incorrect.

- pm June 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why not?
we can use pre-fix tree to implement this.
1) construct empty pre-fix trie in every insert operation
2) find the node in the trie which contains the last character of the substring
3) execute DFS and store all the leaf nodes under that node.

- jellyenigma December 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SuffixTree implements String_DB{
	Node root;
	ArrayList<String> search_result;
	ArrayList<String> search(String s){
		search_result = null;
		Node current = root;
		int index = 0;
		outter : while (current.child.size() != 0){ // until we get to a leaf
			char c = s.charAt(index); // what we are looking for right now 
			inner : for (int i = 0 ; i < current.child.size() ; i++){
				if (current.child.get(i).c == c){
					current = current.child.get(i);
					index++;
					break inner;
				}
				break outter;
			}
		}
		search_result = new ArrayList<String>();
		DFS-Visit(current);
		return result;
	}
	void DFS-Visit(Node current , String so_far){
		String so_far = so_far + current.c;
		if (current.child.size() == 0){
			search_result.add(so_far);
			return;
		}
		for (int i = 0 ; i < current.child.size() ; i++)
			DFS-Visit(current.child.get(i) , so_far);
		return;
	}
	void insert(String str){
		String s = str + "$";
		for (int i = s.length - 1 ; i >= 0 ; i--)
			insert_sub(s.subtring(i));
		return;
	}
	void insert_sub(String s){
		Node current = root;
		int index = 0;
		outter : while (current.child.size() != 0){ // until we get to a leaf
			char c = s.charAt(index); // what we are looking for right now 
			inner : for (int i = 0 ; i < current.child.size() ; i++){
				if (current.child.get(i).c == c){
					current = current.child.get(i);
					index++;
					break inner;
				}
				break outter;
			}
		}
		if (index == s.length() -1) // it's already in the trie
			return;
		while (index < s.length()){
			Node new = new Node(s.charAt(index));
			current.child.add(new);
			current = new;
			index++;
		}
		return;
	}
	class Node{
		ArrayList<Node> child;
		char c;
		public Node(char c){
			child = new ArrayList<Node>();
			this.c = c;
		}
	}
}

- Arian Hosseinzadeh June 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You may also use KMP string matching algorithm , if the length of the list is O(m) and the length of each string is O(n) , this will take O(m * n) and the insertion takes O(1)

ArrayList<String> list = new ArrayList<String>();
ArrayList<String> search(String p){
	ArrayList<String> result = new ArrayList<String>();
	int[] func = prefix_function(p);
	for (int i = 0 ; i < list.size() ; i++)
		if (KMP(list.get(i) , p , func))
			result.add(list.get(i));
	return result;
}

int[] prefix_function(String P){
	int q = 0;
	int[] func = new int[P.length];
	func[0] = 0;
	for (int i = 1 ; i < P.length ; i++){
		while (q > 0 && P[q] != P[i])
			q = func[q];
		if (P[q] == P[i])
			q++;
		func[i] = q;
	}
	return func;
}
boolean KMP(String T , String P , int[] func){
	int q = 0;
	for (int i = 0; i < T.length() ; i++){
		while (q > 0 && T[i] != P[q])
			q = func[q];
		if (T[i] == P[q])
			q++;
		if (q == P.length)
			return true;
	}
	return false;
}

- Arian Hosseinzadeh June 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Also take note that in the previous code , you have to convert string to char array in each of the methods : KMP and prefix_function

void insert(String s){
	list.add(s);
}

- Anonymous June 09, 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