Interview Question


Country: United States
Interview Type: In-Person




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

How about compare two strings using the same iterator i. If any chars[i] is '.' then ignore the equivalence. Else to see chars1[i] == chars2[i].

- Sean January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why "isMatch("hel.") -> false" ?

- Marton January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

. is only 1 char, it can't match hello

- Anonymous January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I used a trie to store the dictionary.

setup is just ordinary routine of building a trie.

isMatch is similar to the ordinary lookup in trie, with addition to check for wildcard '.' in both words in dict as well as the word in query.

public class wildCardMatchList {
	private class trieNode {
		HashMap<Character, trieNode> children;
		boolean wordEnd;
		public trieNode() {
			children = new HashMap<Character, trieNode>();
			wordEnd = false;
		}
	}
	
	private trieNode root;
	
	public void setup(String[] dict) {
		this.root = new trieNode();
		this.root.wordEnd = true; // for empty string
		for (String word:dict) {
			add(this.root, word);
		}	
	}
	
	public boolean isMatch(String word) {
		return match(this.root, word);
	}
	
	private void add(trieNode parent, String word) {
		if (word.isEmpty()) {
			parent.wordEnd = true;
			return;
		}
		
		char letter = word.charAt(0);
		String surfix = word.substring(1); 
		trieNode child = null;
		
		if (!parent.children.containsKey(letter)) 
			child = new trieNode();
		add(child, surfix);
		parent.children.put(letter, child);
	}
	
	private boolean match(trieNode parent, String word) {
		if (word.isEmpty()) return parent.wordEnd;
		
		char letter = word.charAt(0);
		String surfix = word.substring(1);
		
		if (letter != '.') {
			if (parent.children.containsKey(letter) && match(parent.children.get(letter), surfix)) return true;
			if (parent.children.containsKey('.') && match(parent.children.get('.'), surfix)) return true;
		}
		else { // letter == '.'
			for (trieNode child : parent.children.values()) 
				if (match(child, surfix)) return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		String[] dict = { "hello", "mi." };
		wildCardMatchList list = new wildCardMatchList();
		list.setup(dict);
		
		String[] words = {"hello", ".ell.", "hel.a", "hel.", "mix"};
		for (String w : words) {
			System.out.println(w + " : " + list.isMatch(w));
		}
	}
}

Test case:

dict = { "hello", "mi." }

Output:

hello : true
.ell. : true
hel.a : false
hel. : false
mix : true
... : true
.... : false
..... : true

- chriscow January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;


public class StringMatcher {

	HashSet<String> stringSet = new HashSet<String>();
	
	void setup(String[] words){
		if(words != null){
			Collections.addAll(stringSet, words);
		}
	}
	
	 boolean isMatch(String word) {
	
		 if(stringSet.contains(word)){
			 return true;
		 }
		 
		 Iterator<String> stringSetiIterator = stringSet.iterator();
		 
		 while(stringSetiIterator.hasNext()){
			 
			 String current = stringSetiIterator.next();
			 if(current.length() == word.length()){
				 
				 boolean matched = true;
				 for(int i = 0,j =word.length();i< j;i++ ){
					 
					 if(!(current.charAt(i) == word.charAt(i) || current.charAt(i) == '.' || word.charAt(i) == '.')){
						 matched = false;
						 break;
					 }
				 }
				 if(matched){
					 return true;
				 }
			 }
			 
		 }
		 
		 
		 return false;
	}
	 
	 public static void main(String[] args) {
		 String[] dict = { "hello", "mi." };
		 StringMatcher list = new StringMatcher();
			list.setup(dict);
			String[] words = {"hello", ".ell.", "hel.a", "hel.", "mix"};
			for (String w : words) {
				System.out.println(w + " : " + list.isMatch(w));
			}
	}
}

- san4you88 February 03, 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