3 Algorithm Interview Questions For Data Science and Software Engineering in Python | by GreekDataGuy | Towards Data Science

My last data science interview was 90% python algorithm problems.
While you should be prepared to explain a p-value, you should also be prepared for traditional software engineering questions.
Algorithm questions are a learnable skill and companies use them to weed out unprepared candidates.
Below are 3 common algorithm questions and answers, on the easy end of the difficulty spectrum.
An anagram is a string created by rearranging the characters in another string.
Given 2 strings, s1 and s2, return True if they are anagrams of each other.All characters must be used.
Given 2 strings, s1 and s2, return True if they are anagrams of each other.
All characters must be used.
Copy this into a code editor locally and write a function that solves this problem.
def anagram(s1, s2):  #  # your code here  #
Run this to confirm that your function works as expected.
assert anagram('word', 'wodr')assert not anagram('dog', 'dogg')assert anagram('racecar', 'carrace')
This is a solution, but not the only solution.
def anagram(s1, s2):    c1 = {}    c2 = {}        def count_chars(s):        h = {}        for char in s:            if char in h:                h[char] += 1            else:                h[char] = 1        return hreturn count_chars(s1) == count_chars(s2)
Above, we created dictionaries with the count of characters in each string, then compared the dictionaries for equality.
Time complexity is O(n) because iterating over strings and dictionary lookups are dependent on the length of the input strings.
Given a sentence, sent1, and the same sentence with an added word, sent2, return the added word.Every sentence, s1, will have at least 1 word.
Given a sentence, sent1, and the same sentence with an added word, sent2, return the added word.
Every sentence, s1, will have at least 1 word.
def extra_word(sent1, sent2):  #  # your code here  #
assert extra_word('This is a dog', 'This is a fast dog') == 'fast'assert extra_word('answer', 'The answer') == 'The'assert extra_word('Can you solve algorithm questions', 'Can you solve hard algorithm questions') == 'hard'
def extra_word(sent1, sent2):    words = {}        for word in sent1.split(' '):        if word in words:            words[word] += 1        else:            words[word] = 1                for word in sent2.split(' '):        if word in words:            words[word] -= 1        else:            return word
Above, we counted words in the 1st sentence via a dictionary. Then subtracted words in the 2nd sentence from that same dictionary. A word not in the dictionary is the word to be returned.
The time complexity is O(n) because we iterate over each sentence one time.
This is the classic fizzbuzz interview question.
Given n, return a list of values for every integer from 1 through n.If a number is divisible by 3, return 'Fizz' in its place.If the number is divisible by 5 return 'Buzz' in its place.If the number is divisible by 3 and 5, return 'FizzBuzz' in its place.
Given n, return a list of values for every integer from 1 through n.
If a number is divisible by 3, return 'Fizz' in its place.
If the number is divisible by 5 return 'Buzz' in its place.
If the number is divisible by 3 and 5, return 'FizzBuzz' in its place.
def fizzbuzz(n):  #  # your code here  #
output = fizzbuzz(1000)assert output[8] == 'Fizz'assert output[99] == 'Buzz'assert output[198] == 199assert output[510] == 511assert output[998] == 'Fizz'
def fizzbuzz(n):    answer = []    for i in range(1,n+1):        if i % 3 == 0 and i % 5 == 0:            answer.append("FizzBuzz")        elif i % 3 == 0:            answer.append("Fizz")        elif i % 5 == 0:            answer.append("Buzz")        else:            answer.append(i)    return answer
Above, we created a list of values given n. Then iterated over each value and added the value, Fizz, Buzz or FizzBuzz to a list.
Time complexity is O(n) because we iterate over the list one time.
Would you be interested in a series with 5 algorithm questions and answers each week? Let me know in the comments.
As far as algorithm questions go, these were pretty easy and can all be solved in O(n) time complexity. But if you’re new to these types of questions, it’s best to start with the basics.
Algorithm questions will be part of data science and software engineering interviews for the foreseeable future. The best way to stay on top of this skill is doing a couple questions every week. That way you’re always ready if you need to apply to new jobs.