Google Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
Very Simple solution since creating Hash will have space complexity.
Iterate the list and XOR each iterative string with the given string M28K. If they are not 0 then it means it is unique.
An anagram or something similer of the string will also give zero. Which will not lead to an answer.
I dont believe so. Can you write counter example pls? Hey can you share your email ID if possible?
{class M28KStringFinder:
def __init__(self,text):
self.text=text
self.key_str_dict=dict()
self.key_frq_dict=dict()
def getInStr(self):
return self.text
def isKeyUniq(self,key):
if self.key_frq_dict.has_key(key):
if self.key_frq_dict.get(key)==1:
print key + " is unique"
print self.key_str_dict.get(key)
else:
print key + " was not found or not unique"
def populateHash(self):
for string in self.text:
cur_key = string[0]+str(string.__len__())+string[string.__len__()-1]
self.key_str_dict[cur_key]=string
if self.key_frq_dict.has_key(cur_key):
self.key_frq_dict[cur_key]+=1
else:
self.key_frq_dict[cur_key]=1
list1=['a2w3e4r5ts','p0o9i8u7y6t5rs','s23edf4frcf3l','s23edf4fssa3l']
m28KStringFinder = M28KStringFinder(list1)
m28KStringFinder.populateHash()
m28KStringFinder.isKeyUniq('p14s')
I'm think:
# turn the 28 chars in the middle of M28K into bits
bits_target
for s in LIST:
if len(s) == 30 and s[0] == 'M' and s[29] = 'K':
# turn the 28 chars in the middle into bits
bits_current
# do xor for bits_current and bits_target
# if result is 0, they are identical, return False
# otherwise, continue with the next s
# after the LIST is exhausted, return True
Another way to solve this could be:
- Ashupriya August 13, 2012Create a hashmap for the given strings with key as
"FirstLetter Number LastLetter" for example a4t (addict)
and when a query comes we can simply look into this hash map