5 Python Interview Questions & Answers

The Python coding questions challenge your problem-solving and programming skills.

5 Python Interview Questions & Answers
Photo by ThisIsEngineering 


Imagine you are sitting in front of the laptop, listening to the instructions from the hiring manager and reading a coding problem statement. If you are not prepared for a timed coding interview session, you will end up making mistakes.  

So, it is always good to review the most asked questions before sitting for an interview. It will prepare you for tricky, conditional, and advanced coding questions. 


Q1: Odd or Even? Determine that!


Determine if the sum of “n” consecutive numbers is even, odd, or either. 


  • odd_or_even(3) should return “Either”. The sum can be even or odd. 
  • odd_or_even(2) should return “Odd”. The sum of consecutive numbers contains one odd and one even, so the sum will always be odd.
  • odd_or_even(4) should return “Even”. The sum of consecutive numbers contains two even and two odd, so the sum will always be even.


  1. Return “Either” if by dividing the “n” by 2 the remainder is 1. 
  2. If the first condition fails, move to the second condition.
  3. First, divide the number by 2 and again divide it by two to check if it returns the remainder. If the remainder is 1, then the return “Odd” else return “Even”
def odd_or_even(n):
    if n % 2 == 1: 
        return "Either"
    elif (n / 2) % 2 == 1:
        return "Odd"
        return "Even"

To pass this question on the first go, you need to read the description multiple times. The answer is already given in examples. 

It is not the only solution. You can find various solutions on the Codewars platform. You can even come up with your unique solution to gain more points. 


Q2: What is the difference between a tuple and a list?


Both tuples and lists are used for storing and collecting the data. 

The list is declared with square brackets [1,2,3,..], whereas tuples are declared with round brackets (1,2,3,...)

The List is mutable and primarily used for data insertion and deletion. It also has several built-in functions. 

The Tuple is immutable. The iteration is comparatively fast and consumes less memory than a list. It is mostly used for accessing the elements fast. 


Q3: What is a lambda function? Why are they used?


Lambda functions are commonly known as anonymous functions. They do not have names. 

It is used alternatively for regular functions as long as the expression fits into one line. Lambda functions are very simple and light. They improve the code quality.

To create a simple function that adds 20 to the input argument, we will write lambda and provide two components: parameters and expression. 

lambda arguments : expression

The parameter is "a", and the expression is “a+20”. The function can be activated by providing inputs to the “x” variable. 

It's that simple!

x = lambda a : a + 20
>>> 30

We can also provide multiple input arguments to the function. In our case, it has multiplied 2 and 5 to print 10. 

x = lambda a, b : a * b
print(x(2, 5))
>>> 10

Q4: Maximum 69 Number


You are provided with a positive integer “num”. It consists of only 6 and 9 digits.  

Return the maximum number by changing one digit (6 or 9).


  • Input: num = 9669, Output: 9969
  • Input: num = 9996, Output: 9999
  • Input: num = 9999, Output: 9999


To get the largest number, we have to always change 6 to 9, and it should be the first digit (6).

Let’s say we have num = 9669 

  • Changing the first digit will result in 6669. 
  • Changing the second digit will result in 9969.
  • Changing the third digit will result in 9699.
  • Changing the fourth digit will result in 9666.

9969 is the highest number. 

We just have to create a simple function that will convert one from 6 to 9 at the start. 

For that, we will convert the number to a string and use the `.replace` function to replace “6” with “9”. The third argument is "count" with 1. It will only replace one digit. After replacing we will convert the string to an integer. 

def maximum69Number(num):
    return int(str(num).replace('6','9',1))
num = 966696696

>>> 996696696

Q5: Who wins first?


Question asked during Microsoft’s coding interview:

Amy and Brad take turns in rolling a fair six-sided die. Whoever rolls a “6” first wins the game. Amy starts by rolling first. What’s the probability that Amy wins?

Before we jump into the solution part, I want you to read the question again. It is a simulation question, and we will be finding the probability Amy wins. 

Amy will always roll the dice first, and if she hits the 6 on the first try she wins. Otherwise, Brad turns to roll the dice, and if he gets 6, he wins. If not, then the turn will shift back to Amy until one of them is a winner. 

This question is all about iteration and logic. 

  1. Initialize the A_count and B_count with zero. We will be using them to create probability. 
  2. After that, we will run the simulation for “size”. In our case, it is 1000.
  3. With the help of the Numpy random randint function, we will generate a number from 1 to 6. The randint function requires low (inclusive) and high (exclusive) numbers. You can even use a random library instead of Numpy. 
  4. If the random number is 6, add one to A_count (Amy) else roll again for Brad. 
  5. If the B_role hits 6, add one to B_count (Brad). Else move to Amy.
  6. This cycle will run for thousand iterations, and we will collect the Amy and Brand winning to calculate the probability. 
  7. To calculate the probability of Amy winning, we will divide the number of times Amy got 6 divided by the total winning of Amy and Brad. 
import numpy as np

def win_probability(size):

    A_count = 0 
    B_count = 0 
    for i in range(size): 
        A_role = np.random.randint(1,7)
        if A_role == 6:       
            B_role = np.random.randint(1,7)
            if B_role == 6:   
    return A_count/(A_count+B_count)

For reproducibility purposes we will be using Numpy’s seed.

>>> 0.5746031746031746

As we can see, Amy has the upper hand over Brad as she starts first. She has a win probability of 57.5 % over 1000 iterations. 




  1. Odd or Even? Determine that! | Codewars
  2. What is the difference between a tuple and a list? | Codecademy 
  3. What is a lambda function? Why are they used? | Codecademy 
  4. Maximum 69 Number | LeetCode
  5. Who wins first? | Leihua Ye, PhD

Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master's degree in Technology Management and a bachelor's degree in Telecommunication Engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.