Sign in

Data Scientist | B.S. in Information Analysis from University of Michigan School of Information | linkedin.com/in/robertalterman/ | github.com/ralterman

A breakdown of a common Python problem given in interviews

Photo by Karolina Grabowska from Pexels

Write a function that calculates the sum of two integers without using the + or – operators…

def get_sum(a, b):
nums = []
nums.append(a)
nums.append(b)
return sum(nums)

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that the function will be taking in two numbers, be sure to include two arguments in your function declaration that will represent those numbers.

Step 2: Create an empty list

After reading the prompt, you should be thinking to yourself: “How can I calculate a sum in Python without using + or –?” My first thought is to use the sum function


Choosing the more appropriate descriptive statistic

Sneaker Order Prices

The mean and the median are two of the most common features used when describing numerical data. The two are known as measures of central tendency, meaning they describe a set of data by shedding light on the central position of the data. The mean is the average value — it’s the value that you get when you add up all of the data and divide that number by the number of points in the dataset. On the other hand, the median is the middle number in a set of data once it has been ordered from smallest to largest.


A breakdown of a common Python problem given in interviews

Photo by Lara Santos from Pexels

Write a function that when given a number, prints the square of each preceding non-negative integer…

def squares(n):
for num in range(0, n):
print (num**2)

*Definitely on the easier side!

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that the function will be taking in one number (n), be sure to include one argument in your function declaration that will represent that number.

Step 2: Write your for loop

In this case, a for loop is needed due to the fact that the prompt says you’re squaring all of the non-negative integers that precede the input…


A breakdown of a common Python problem given in interviews

Image by Mike Dibos from Pixabay

Write a function that takes in a year and returns ‘True’ or ‘False’ whether that year is a leap year. In the Gregorian calendar, three conditions are used to identify leap years:

  • The year can be evenly divided by 4, is a leap year, unless:
  • The year can be evenly divided by 100, it is NOT a leap year, unless:
  • The year is also evenly divisible by 400. Then it is a leap year.
def is_leap(year):
leap = False
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
leap = True
else:
leap = True
return leap

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name…


A breakdown of a common Python problem given in interviews

Photo by Boris Smokrovic on Unsplash

Write a program that outputs the string representation of numbers from 1 to n as a list of strings. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. — LeetCode

def fizzBuzz(n):
numbers = []
for num in range(1,n+1):
if (num%3 == 0) and (num%5 == 0):
numbers.append('FizzBuzz')
elif num%3 == 0:
numbers.append('Fizz')
elif num%5 == 0:
numbers.append('Buzz')
else:
numbers.append(str(num))
return numbers

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that the function will be taking in one number (n), be sure to include one argument in your function declaration that will represent that number.

Step 2: Create an empty list

After declaring your function, you should create an empty…


A breakdown of a common Python problem given in interviews

Image by inspirexpressmiami from Pixabay

Write a function to check if a substring is present in a given string…

def is_present(string, substring):
if string.count(substring) > 0:
print ("Yes, the substring IS present.")
else:
print ("No, the substring is NOT present.")

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that you will be comparing two strings, be sure to include two arguments in your function declaration that will represent each string — the substring and the full string.

Step 2: Write your conditional statement

The key to solving this problem is figuring out how you can check if a string…


A breakdown of a common Python problem given in interviews

Write a function to check if two strings are anagrams* of each other…

*An anagram is a word formed by rearranging the letters of another word

def anagrams(string1, string2):
if sorted(string1) == sorted(string2):
print ("These strings ARE anagrams of each other.")
else:
print ("These strings ARE NOT anagrams of each other.")

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that you will be comparing two strings, be sure to include two arguments in your function declaration that will represent each string.

Step 2: Write your conditional statement

The key to solving this problem…


A breakdown of a common Python problem given in interviews

Image by Gerd Altmann from Pixabay

Write a function that takes in a list and returns the duplicate items in that list…

def duplicates(the_list):
dups = []
for item in the_list:
if (the_list.count(item) > 1) and (item not in dups):
dups.append(item)
return dups

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you the function will take in a list, be sure to include one argument in your function declaration that will represent this list.

Step 2: Create an empty list to hold the duplicate items that you’re going to be returning

Due to the fact that your input list might have more than one duplicate item, you’re going to need something to hold onto…


The almighty git mv command

Photo by Ross Findon on Unsplash

As a coder, you’ve likely been in the following position. You’re starting a new project and when it comes time to name your file you go with a name that you believe serves as good insight as to what the file contains. After doing some work in this file and committing it to GitHub multiple times, your file begins to resemble something not quite like its name suggests. If you choose to leave the file name the way it is, other people who check out your GitHub page can easily be misled, as its contents do not reflect its name.


Exploring a common and intriguing probability theory

Photo by Ylanite Koppens from Pexels

The birthday problem (also know as the ‘birthday paradox’) is a probability theory which states that in a set of n randomly chosen people, at least two of them will have the same birthday. While this probability is 100% when there are at least 367 people, as there are 366 possible birthdays (including February 29th), the theory holds a 99.9% probability when there at least 70 people and even 50% when there are only 23 people involved.

While at first this might seem crazy to think that with just 23 people there’s a 50% probability that at least two of…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store