Python Developer Interview Questions

Python is kind of trending language these days with its wide range of applications from science to web development and so on. If you’re looking for a job as a Python developer or if you’re interviewing candidates for a role on your team, this tutorial will come in handy. Firstly, Python Developer means Software Developer specifically in Python language and its related Frameworks of working with a team. We’ll divide this section of interviewing a Python developer into four parts:
  • Common software engineering skills
  • Python language specific questions
  • Python related framework, library (related to the product/project we are working on)
  • Non-technical questions
First, in order to get to know more about the candidate you’re interviewing, we will start with some non-technical questions. These can range from asking him or her to introduce themselves, to describe some of his/her projects in the past, what he/she likes to do in their daily life or free time, what they hope to get out of this experience for the future and so on. Once your interviewee has had the chance to introduce themselves, it’s time for you to tell them more about the project or more permanent role on the team. Tell them what types of project or projects you’ll be working on, their teammates and what they can expect during their time working with you. After that, it’s time to jump into some important concepts to gain a better understanding of their knowledge and experience in software development.

Common Software Engineering Skills

Common and important concepts

We’d recommend asking your candidates some important concepts in the realm of software engineering such as:
  • Big-O:

Big-O is a common concept used in computer science. It is often used to classify algorithms according to how their running time or space requirements grow as the input size increases. It tells users about the performance or complexity of an algorithm. The candidate should know about Big-O and know how to analyze at least simple cases of algorithm complexity. Understanding Big-O and Big-O analysis affects how a candidate writes his code. We recommend showing your candidate a line of code and ask them about that code’s complexity. For example:
def contains_duplicates(elements):
   for i, outer in enumerate(elements):
       for j, inner in enumerate(elements):
           # Don't compare with self
           if (i == j): continue
           if (outer == inner): return True
   return False
  • Common algorithms and data structures:

Data structures and algorithms are like the brain of any program. In general, a developer must have knowledge of some common algorithms and especially data structures. We recommend asking candidates to describe some important some of these important data structures such as: Set, Hashset, Dictionary, Array, Tree, List, Stack, Queue, etc. Algorithms like: DFS, BFS, Linked List Insertion, Binary Search, etc.
  • Debugging with an interactive debugger:

A developer typically works with a debugger even more so than writing code. Working well with a debugger helps the developer to identify problems/bugs much quicker. He or she should know about breakpoint, step over, step into, conditions, actions.

Relational and non-relational databases:

Database is an integral part of software. Here, we will ask the candidate to talk about the characteristics and  benefits of relational and non-relational databases and when we should use them.
  • Relational:

According to techopedia, relational databases: Organize data in different ways. Each table is known as a relation, which contains one or more data category columns. Each table record (or row) contains a unique data instance defined for a corresponding column category. One or more data or record characteristics relate to one or many records to form functional dependencies. What defines Relational Database is revealed by these 5 keywords:
  1. Table
  2. record
  3. column
  4. relation
  5. index.
Go for RDBMS when the data is well structured and lends itself to a tabular arrangement (rows and columns) in a relational database. Consider whether the data is transactional in nature. The concept of ACID transactions is very important and any RDBMS.
  • Non-relational databases:

Techopedia defines non-relational databases as:
A database that does not incorporate the table/key model that relational database management systems (RDBMS) promote. These kinds of databases require data manipulation techniques and processes designed to provide solutions to big data problems that big companies face. The most popular emerging non-relational database is called NoSQL (Not Only SQL).
Go for NoSQL databases when there is no fixed (and predetermined) schema of data fits in. In NoSQL, Scalability, Performance (high throughput and low operation latency), Continuous Availability are very important requirements to be met by the underlying architecture of the database. NoSQL can store enormous amounts of data in the range of petabytes.
  • Database index

The indexing technique is an important concept in Relational Databases. By using index in the correct way, it will boost the performance of retrieval data from a database dramatically. But in contrast, if index is used incorrectly, the performance is not improved at all, but the storage required to store indexes is increased without any benefit. The candidate should know what database index is and how Indexing works in RDBs.
  • Automated Unit Testing

Unit testing is the process of checking that the program works and continues to work as the developer intended. Automated unit testing allows any developer to verify that their current changes do not break the existing code, so it makes the code base more robust. A developer should be familiar to write automated unit tests and do it in their daily coding.
  • Version control

Being a developer in a team, it’s important to synchronize the code base between developers and to keep track of changes over time. One developer on the team may be working on a new feature while another developer fixes an unrelated bug. Each developer may make changes in several parts of the file tree. Version control helps teams solve these kinds of problems by tracking every individual change by each contributor and helping to prevent concurrent work from conflicting with each other. A developer should be able to work well in version control systems such as SVN or Git.
  • SOLID DRY KISS

These principles are basic engineering principles that each software developer, more specifically Python developer should know. DRY: In software engineering, Don’t Repeat Yourself (DRY) or Duplication is Evil (DIE) is a principle of software development KISS: KISS is an acronym for the design principle “Keep it short & simple!” SOLID: When applied together, these principals intend to make it more likely that a programmer will create a system that is easy to maintain and to extend over time.
  • Regular expression

The regular expression is a powerful tool when working with text. It makes the check and returns if it finds any text that matches your expression pattern. Since most of these programming features deal with a lot of text, regex helps in performing validations against your strings/text of your interest in programs.
  • Software Architecture and Design Patterns

A developer should know about some common patterns including Architectural Patterns like: Client-server, MVC, MVVC, Event-bus etc. Design Patterns like: Singleton, Factory, CHAIN OF RESPONSIBILITY etc. Knowing these common Architecture and design patterns are not only for creating a code base, but also for quickly understanding the code base of a project that the developer is involved in.

Problem Solving Ability

During the interview process it’s important to evaluate your candidate’s ability to solve complex problems. Not simply if they can solve a problem but also to understand how they came to the solution. Ask the candidate to solve some quick problems so you can evaluate how the candidate approaches and solves each issue. As the interviewer, you should assessing how quickly they arrived to a good solution and how good that solution actually was. Below is a sample question: Given a string, find the length of the longest substring without repeating characters. Additional examples:
  • Given “abcabcbb”, the answer is “abc”, which the length is 3.
  • With “bbbbb”, the answer is “b”, with the length of 1.
  • Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring,
  • “pwke” is a sub-sequence and not a substring.
Answer:
def longest_substring(s):
   char_dict = {}
   longest_count = 0
   for i, c in enumerate(s):
       if c in char_dict:
           longest_count = max(longest_count, len(char_dict))
           dup_i = char_dict[c]
           char_dict = {k: v for k, v in char_dict.items() if v > dup_i}
       char_dict[c] = i
   return max(longest_count, len(char_dict))
Additional questions similar to above can be found here.

Python Language Specific Questions

Question 1: What is Python and what are the key features of Python?

Some key points:
  • Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of using Python are that it is simple and easy, portable, extensible, built-in data structure and it is an open source.
  • Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.
  • Python is dynamic but strong typing, meaning that you don’t need to state the types of variables when you declare them or anything along those lines.
  • You can do things like x=1 and then x=”A string” without error. But when you manipulate variables with different types, it will generate errors, unlike C# or Java: print(“A” + 1) # TypeError: must be str, not int
  • In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first-class objects. This is similar to Javascript.
  • Python is used widely from web applications, automation, scientific modeling, big data applications and many more. But it is famous for machine learning and data science. Python is one of the most popular programming languages used by data analysts and scientists.

Question 2: What is dictionary in Python?

A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It defines one-to-one relationships between keys and values. Dictionaries contain a pair of keys and their corresponding values. Dictionaries are indexed by keys. An important feature of a dictionary is the complexity of getting and setting an element is O(1). For example:
new_computer={'Chipset':'8th-generation Intel Core i7 processor','Card':'Radeon Pro 560X','Ram':'16GB 2400MHz DDR4', 'Storage': '512GB SSD'}
print(new_computer['Chipset'])
'8th-generation Intel Core i7 processor'

Question 3: Looking at the below code, write down the final values of A0, A1, …An.

A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
A1 = range(10)
A2 = sorted([i for i in A1 if i in A0])
A3 = sorted([A0[s] for s in A0])
A4 = [i for i in A1 if i in A3]
A5 = {i:i*i for i in A1}
A6 = [[i,i*i] for i in A1]
Answer:
A0 = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}  # the order may vary
A1 = range(0, 10) # or [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] in python 2
A2 = []
A3 = [1, 2, 3, 4, 5]
A4 = [1, 2, 3, 4, 5]
A5 = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
A6 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
List comprehension is an important task developers with work with every day. Not only Python but also in many other languages. So, this is a must-ask question to see if the  candidate is truly familiar with Python.

Question 4: How is memory managed in Python?

Memory management in Python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The Python interpreter takes care of this instead. The allocation of heap space for Python objects is done by Python’s memory manager. The core API gives access to some tools for the programmer to code. Python also has an inbuilt garbage collector, which recycles all the unused memory so that it can be made available to the heap space.

Question 5: What is monkey patching in Python?

In Python, the term ‘monkey patch’ only refers to dynamic modifications of a class or module at run-time. Consider the following example:
# m.py
class MyClass:
def f(self):
print "I am f()"
We can then run the monkey-patch testing like this:
import m
def monkey_f(self):
print "I am monkey_f()"
   
m.MyClass.f = monkey_f
obj = m.MyClass()
obj.f()
The output will be I am monkey_f(). As we can see, we did make some changes in the behavior of f() in MyClass using the function we defined, monkey_f(), outside of the module m.

Question 6: What are negative indexes and why are they used?

The sequences in Python are indexed and it consists of the positive as well as negative numbers. If you know Ruby, it’s kind of similar. It can be compared to the way of counting forward and backward in a circle. The index for the negative number starts from ‘-1’ that represents the last index in the sequence and ‘-2’ as the penultimate index and the sequence carries forward like the positive number. The negative index is used to remove any new-line spaces from the string and allow the string to except the last character that is given as S[:-1]. The negative index is also used to show the index to represent the string in the correct order.

Question 7: What is the difference between range and xrange?

In Python 2, range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements otherwises xrange is a sequence object that lazily evaluates. But in python3, range does the equivalent of Python’s xrange.

Question 8: Consider the following code, what will be its output?

class A(object):
   def go(self):
       print("go A go!")
   def stop(self):
       print("stop A stop!")
   def pause(self):
       raise Exception("Not Implemented")
class B(A):
   def go(self):
       super(B, self).go()
       print("go B go!")
class C(A):
   def go(self):
       super(C, self).go()
       print("go C go!")
   def stop(self):
       super(C, self).stop()
       print("stop C stop!")
class D(B,C):
   def go(self):
       super(D, self).go()
       print("go D go!")
   def stop(self):
       super(D, self).stop()
       print("stop D stop!")
   def pause(self):
       print("wait D wait!")
class E(B,C): pass
a = A()
b = B()
c = C()
d = D()
e = E()
# specify output from here onwards
a.go()
b.go()
c.go()
d.go()
e.go()
a.stop()
b.stop()
c.stop()
d.stop()
e.stop()
a.pause()
b.pause()
c.pause()
d.pause()
e.pause()
Answer:
a.go()
# go A go!
b.go()
# go A go!
# go B go!
c.go()
# go A go!
# go C go!
d.go()
# go A go!
# go C go!
# go B go!
# go D go!
e.go()
# go A go!
# go C go!
# go B go!
a.stop()
# stop A stop!
b.stop()
# stop A stop!
c.stop()
# stop A stop!
# stop C stop!
d.stop()
# stop A stop!
# stop C stop!
# stop D stop!
e.stop()
# stop A stop!
a.pause()
# ... Exception: Not Implemented
b.pause()
# ... Exception: Not Implemented
c.pause()
# ... Exception: Not Implemented
d.pause()
# wait D wait!
e.pause()
# ...Exception: Not Implemented
Because OO programming is extremely important in today’s industry. Correctly answering this type of question shows that your candidate understands the inheritance and the use of Python’s super function. python_developer

Conclusion

And there you have it. The topics covered in this tutorial will help you to separate the experienced from the not so experienced while interviewing candidates to join your team as well as provide strong preparation material for those interviewing for a Python developer role.
Share this article:
You may be interested in these articles