Monday, July 20, 2020

Questions for a Python interview



In this post I have collected few questions for a Python based interview.

The goal of these questions is not to check whether the candidate knows the bits and bytes of Python (or of any language). 

Instead, the purpose is to find out the ability of the candidate to understand general programming issues that are related to most programming languages, using the Python language as a tool to express her/his ideas.

The related questions and answers are listed below.


Tuple vs. List


Question: 

What are the differences between Tuple and List? 
Provide an example of when should we use each of them.

Answer:

1.
A list is a mutable object (it can be modified).
A tuple is an immutable object (it cannot be modified), but the variable can be assigned a new tuple.

2.
A tuple memory footprint is slightly lower than a list memory footprint.

3.
Unlike a list, the tuple provides __hash()__, and hence can be using as a key in a dictionary.

4.
We use a tuple to describe an object properties, such as an employee details, and we want to emphasize that these properties cannot be modified. 
This is similar to dictionaries, bur without keys, and using only values.
For example:
('Joe Doe', 'accounting', 75000)


List Comprehension


Question: 

1.
Write a function that given a list of words (strings), uses a list comprehension to sum the lengths of all of the words starting with the character 'a'.
For example: 

print(a_len(['a', 'b', 'c', 'another', '', 'all']))

would print 11.

2.
What if you have a static very long list of words, and the function is called millions of times using a different prefix required instead of the 'a' prefix. How can you change the implementation?

Answer:

1.
The function is:

def a_len(words):
return sum([len(w) for w in words if w.startswith('a')])


2.
Assuming that we can do some pre-process work, we can create a trie from the list, for example using a depth of 5 characters, then, each leaf of the trie would hold the relevant list of words after the leaf prefix filtering, and hence the final work would be to filter and sum a relatively smaller amount of words.

This required a lot of memory. In case this is a problem, we can use files to represent the trie, or use a cluster based database, such as Redis, that can span across multiple servers.


Generators


Question: 

What is a generator, and what is the benefit of using a generator over using a list?

Answer:

A generator is a function that behaves like an iterator.
It uses the yield keyword to produce the next iteration element.
Using a generator is better than a list as it does not allocate the entire list elements in the memory, but instead produces a single element every time. 
This allows a streaming process implementation for the iteration, and reduces memory footprint.


Unit Test


Question: 

How can we write a unit test in python?
What is the difference between a unit test and a system test?

Answer:

A unit test is a set of functions that check the production python code.
The unit test can be based on the assert keyword, and on proprietary implementation to validate the code. In addition, a tests framework (such as unittest, nose, or pytest) is used to run the tests.

A unit test should not depend on external resources, such as databases, other microservices, for its operation. It should use mocks to simulate APIs with any external resources.

A unit test should target a validation of a small piece of the code, such as one function, one class or one file.

Unlike a unit test, the system test should test the entire product, end to end.
It will use real databases, and external entities.
It will address the validation method from the user point/requirement of view, and not from the code/module point of view



No comments:

Post a Comment