Amazon Interview Question
Software Engineer / DevelopersTeam: Audible
Country: United States
Interview Type: Phone Interview
With this method you will need to be mindful of the length. So you need to keep track of it when traversing the tree
Well there are a few clarifications needed -
How big is the file?
Are the strings in file sorted?
Does "_" mean single character or multiple characters?
If big file and strings are not sorted and we are using this function a lot then it makes sense to make a suffix tree like structure.
If sorted then just need to reach that point using some variation of binary search.
If small file then maybe even using brute force way of searching may be benificial.
Does underscore ('_') imply one character, or can it be multiple characters, including no character?
If the former, then scan the file for all 4 character words starting with H, having L as the 3rd character.
Building a trie will be rather expensive, not to mention that it may not all fit into memory in the first place. Building a distributed trie is possible to workaround this, but that infrastructure investment better be worth the returns on it.
Using Python
words=['help','hell','bug','hale','gale']
s="h_l_"
for w in words:
if len(w)!=len(s):
print '%s does\'t match'%w
else:
match=True
for i in range(len(s)):
if s[i]=='_':
continue
else:
if s[i] != w[i]:
match=False
break
if match:
print '%s matches'%w
else:
print '%s dosn\'t match'%w
Scala:
Assuming the words have been read into a List:
def getPossibleSolns(words: List[String],r: Set[String]): Set[String] = words match {
case Nil => r
case head :: tail => head.toList match {
case 'H' :: _ :: 'L' :: _ => getPossibleSolns(tail,r+(head))
case _ => getPossibleSolns(tail,r)
}
}
val xs = List("HOW","WHAT", "HELP", "WHERE", "HELD","HOW","HELL")
val r = getPossibleSolns(xs,Set())
scala> val r = getPossibleSolns(xs,Set())
r: Set[String] = Set(HELP, HELD, HELL)
We can use shell scripting... Such kind of pattern searching is much easier in scripting language.
@Varun: if you really want to do the pattern search then you can also use regular expressions (in Java, C#), In that case programatically creating the pattern itself will be the only tough part. personally i liked the trie way assuming you don't have any APIs (which is what the interviewer must be expecting)
build a trie from the file
- dgbfs November 20, 2012traverse the trie based on the pattern
if it is specific character, traverse the edge of that character
if it is _, traverse all edges