Introduction
Today I’m going to discuss with you Advanced Python Interview questions that you can use in your interviews or come back here to practice and try to remember. This will be an ever growing guide but at the same time I will try to be selective. I do not want to put material here that’s repeated a lot on other sources. Most of the content will be unique and catchy so you can outwit yourself or interviewees!
Over the course of the last 23 years of being a developer, manager, architect and systems engineer I have had to interview over 250 people. In most of the roles I was in the main programming language and environment was Python. The useage varied from server-side code, graphics applications, machine learning and just general scripting. One thing that I learned is good knowledge of the fundamentals is what makes the difference between an average programmer and a good one.
I always try to stick to the fundamentals when I’m interviewing someone. Also after a while you start seeing patterns in the interview process. Certain characteristics of people come out quickly for example if someone is a problem solver, an over-analyzer, the person that just wants to write code etc. In this article I’m going to try to break down some specific questions regarding Python. These will not be in general nature problem solving tasks such as those found in websites such as leetcode. As side note you should be practising in leetcode if you want to be able to do well in logarithmic problems.
As part of this blog I have uploaded all the code in the git repository which includes references to everything seen below. If something is missing please reach out to me and I will make sure to include it. You can find the git repo for it here.
Questions
How does Python Inheritance work
The following items need to be demonstrated for the code below:
- Be able to explain the method order of the class inheritance
- How functions get affected if they have the same name
- Be able to explain what output the below program will have
class Base1:
def __init__(self) -> None:
print ('This is base1 init')
def which_one_executes(self):
print ('Base1 execution')
class Base2:
def __init__(self) -> None:
print ('This is base2 init')
def which_one_executes(self):
print ('Base2 execution')
class Main(Base1, Base2):
def __init__(self) -> None:
super().__init__()
print ('This is main')
self.which_one_executes()
main_obj = Main()
main alex@DYNAMH ~/code/unbiased-coder/python-interview-questions > python ./mro.py
This is base1 init
This is main
Base1 execution
What is the difference of an anonymous function and a tuple in Python
Things that need to be demonstrated here are as follows:
- Understand what an anonymous Python function is
- Understand what a set is and how it’s declared
- Be able to provide an explanation of how each can be initialized using the the set() or lambda declaration (bonus question)
- Be able to guess the output of the print out functions
example1 = (1)
example2 = (1,)
print ('Example1: ', example1, type(example1))
print ('Example2: ', example2, type(example2))
main alex@DYNAMH ~/code/unbiased-coder/python-interview-questions > python ./tuple_anon_func.py
Example1: 1
Example2: (1,)
What does the following Python Lambda expression return
In this example you will be presented with a dynamic lambda expression and the expectation is to guess the output of what it will be showing if the code gets executed.
The interviewee should successfully demonstrate the following things:
- Explain why the result was the value derived
- What does an anonymous lambda python casting do
- How does python treat Booleans in arithmetic
(lambda x,y,z: x+y+z)(1,2,3)-True+False
- When doing Boolean arithmetic the Booleans are converted using int so False would be 0 and True will be 1
- The lambda brackets allows you to supply arguments on the fly when executing as a one-liner
- The evaluation of the lambda result is 6 since it's a simple addition and the Booleans and the values shown above so only True affects the operation
What does the Sum function return when executed
In this example you will be presented with some sample code of the sum function being called. Your task is to say what the output is going to be:
Some things you need to explain are:
- How you came to the conclusion of the expected output
- What happens when the sum function is being called internally with Python
sum = 1
sum += sum([1,2,3,4,5])
print ('The sum is: ', sum)
Traceback (most recent call last):
File "", line 1, in
TypeError: 'int' object is not callable
Write a Fibonacci Function using a one-liner using lambda
Things that need to be demonstrated here:
- Code should be 1 line the reason for this is to show conditional expression matching in the same line
- The use of a lambda function could be leveraged to accomplish this
- The use of function recursion needs to be demonstrated here
- The use of recursion exists as we call ourselves and by ourselves I mean the lambda function
- The use of conditional expressions is being made as a one-liner which demonstrates the ability to do that quickly
fib = lambda limit: limit if limit < 2 else fib(limit-1) + fib(limit-2)
print ('Fib(20): ', fib(20))
print ('Fib(10): ', fib(10))
main alex@DYNAMH ~/code/unbiased-coder/python-interview-questions > python ./fib.py
Fib(20): 6765
Fib(10): 55
What is the difference between a classmethod and a staticmethod in Python
The interviewee needs to demonstrate a good understanding between the two concepts which are not very common in Python programming along the things below:
- Explain where classmethods are being used
- Explain the difference between the two
- Explain where staticmethods are being used
- Explain what is the advantage of not instantiating a class object and calling into routines directly
class TestClass(object):
def __init__(self) -> None:
super().__init__()
@classmethod
def test_class_method(cls):
print ('this is a class method')
print ('instance is: ', cls)
@staticmethod
def test_static_method():
print ('this is a static method')
print ('static methods have no instances')
def test_regular_method(self):
print ('this is a regular method')
print ('instance is: ', self)
TestClass.test_static_method()
TestClass.test_class_method()
try:
TestClass.test_regular_method()
except TypeError:
print ('Failed executing regular method, regular methods need an instance to be executed')
t = TestClass()
t.test_regular_method()
- What happens with a regular class function
- What happens with a classmethod decorated function
- What happens with a staticmethod decorated function
main alex@DYNAMH ~/code/unbiased-coder/python-interview-questions > python ./difference_class_static_method.py
this is a static method
static methods have no instances
this is a class method
instance is:
Failed executing regular method, regular methods need an instance to be executed
this is a regular method
instance is: <__main__.TestClass object at 0x6fffffe9af10>
Conclusion
If you are looking to hire someone please consider a holistic approach. Assemble a proper list of questions ahead of time and just include those are bonus tips to see how well he performs. Not everyone needs to score super high and not everyone needs to be proficient in every little detail a language has. A common problem is that a lot of interviewers get caught out on scoring but miss a lot on how the person is thinking and analyzing a problem. Some solutions which can easily be google’d don’t need to weigh heavily on your decision of whether or not to hire a candidate. Do you have any other interesting questions to offer to me please put in the comments below I’d be interested to hear about them and update my guide. Did I make any mistakes please let me know about that too!
Python interview questions can be really easy and really complex this guide was meant for those looking to push their limits.
If you are looking on some more information on Python in general I have a section of articles here.
Some common ones that you may enjoy can be found here: