This chapter will help you to get answers on typical interview questions for all type of developers. This is Algorithmic questions that you will hear on your next interview. Be prepared for this type of questions in advance, because it's not easy questions and you need to understand it, not only read answers.
I've created a mini book about algorithms to help you be prepared to the algorithmic questions.
Read Complexity function and Big O notation.
code here# general language-independent implementation
def find_substr(string, substr):
"""Check if string contains substring.
>>> find_substr('', '')
True
>>> find_substr('', 'a')
False
>>> find_substr('a', '')
True
>>> find_substr('a', 'a')
True
>>> find_substr('a', 'aa')
False
>>> find_substr('aa', 'a')
True
>>> find_substr('abc', 'abc')
True
>>> find_substr('abcd', 'abc')
True
>>> find_substr('ababc', 'abc')
True
>>> find_substr('ababcd', 'abc')
True
>>> find_substr('abcabc', 'abcd')
False
"""
for i in range(0, len(string) - len(substr) + 1):
for j in range(0, len(substr)):
if substr[j] != string[i + j]:
break
else:
return True
return False# pythonic implementation with string slice
def find_substr(string, substr):
return any(substr == string[i:i + len(substr)] for i in range(len(string) - len(substr) + 1))Given string and we need to return all permutations of this string.
def permute_string(original_string):
"""
>>> permute_string('')
[]
>>> permute_string('a')
['a']
>>> permute_string('ab')
['ab', 'ba']
>>> permute_string('abc')
['abc', 'bac', 'bca', 'acb', 'cab', 'cba']
"""
results = []
if len(original_string) == 0:
return results
ch = original_string[0]
substrings = permute_string(original_string[1:])
if not substrings:
results.append(ch)
for substring in substrings:
for index in range(len(substring) + 1):
tmp_str = substring[0:index] + ch + substring[index:]
results.append(tmp_str)
return results