owned this note
owned this note
Published
Linked with GitHub
# Python questions for interviews
## Easy
### Question 1
Can you tell the difference between unit, functional, and integration tests ?
Functional testing is defined as the testing of complete functionality of some application. Integration tests, test how parts of the system work together and unit tests test the small pieces of the application.
Comments
### Question 2
Given the following list:
```
words = ["one", "one", "two", "three", "three", "two"]
```
how can you display only the unique elements of the array
Possible answer:
```
list(set(words))
```
### Question 3
What’s a list comprehension ?
### Question 4
What's the difference between:
```
x = [i for i in range(10)]
```
and
```
x = (i for i in range(10))
```
One create a list with 10 elements starting in 0, the other create an iterator
### Question 5
Which one is better, and why use one or another
Iterator is used when you only need to calculate the next value in real time, it's faster depending on what is inside the list, the list has all the elements. Iterator is also fixed values in memory while lists increase depending on the number of elements.
### Question 6
What __getattr__ do?
### Question 7
What is a decorator in python
### Question 8
I have a function in python that download a very large glance image calculates the md5sum and return it, what's the best way to test this function without need to download the image
```
Using Mock
```
### Question 9
What are local variables and global variables in Python?
Global Variables: Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.
Local Variables: Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.
### Question 10
What is the difference between Python Arrays and lists?
Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.
### Question 11
What is monkey patching?
Dynamic modifications of a class or module at run-time refers to a monkey patch.
## Medium / Advanced
### Question 1
How do you sort a Python Snake sequence when the ordered is induced by *another* sequence?
(For example, how do you sort a list of names by the age of each person?)
Example
```
names = ["A", "B", "C", "D"]
ages = [21, 35, 18, 78]
```
Answer
```
sorted(zip(ages, names))
```
```
ordered_names = [name for _, name in sorted(zip(ages, names))]
ordered_names
```
### Question 2 (tricky)
You have the following:
```
names = ["A", "B", "C", None]
ages = [20, 30, 40, 20]
```
What's the output of this:
```
(23, "A") < (20, None)
False
(19, "A") < (20, None)
True
(20, "A") < 20, None)
TypeError exception
```
What would happen if you sort it with zip?
```
sorted(zip(ages, names))
TypeError exception
```
### Question 3 (tricky)
What's the output of this code:
```
a = 3
l = [3, 5]
if a in l == True:
print("Yeah")
else:
print("What?")
```
Explanation:
`in` is a comparison operator, meaning the expression corresponds to 2 comparisons that have been chained, much like `2 < 4 < 7`is `2 < 4 and 4 < 7`.
## Hard (but easy)
### Question 1
How can you calculate the triangular number?
For example, the triangular number for 3 is 6:
```
# *
# * *
# * * *
```
Possible sollution
```
def triangular_number(n):
acc = 0
for i in range(n + 1):
acc += i
return acc
```
Better sollution
```
def triangular_number(n):
return sum(range(n + 1))
```
However, this is a linear function, this can be better:
```
def triangular_number(n):
if n == 1:
return 1
return n + triangular_number(n - 1)
```
But what's the issue with this?
```
triangular_number(1023)
```
```
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in triangular_number
File "<stdin>", line 4, in triangular_number
File "<stdin>", line 4, in triangular_number
[Previous line repeated 995 more times]
File "<stdin>", line 2, in triangular_number
RecursionError: maximum recursion depth exceeded in comparison
```
The sum of 1 + 2 + ... + N can be simplified to N * (N + 1) / 2:
```
def triangular_number(n):
return n * (n + 1) / 2
```
But the output now is a float:
```
>>> triangular_number(10_000_000)
50000005000000.0
```
Resolving casting int:
```
def triangular_number(n):
return int(n * (n + 1) / 2)
```
Tends to rounding errors:
```
>>> triangular_number(10_000_000_000)
50000000005000003584
```
What's the sollution then? Use //:
```
def triangular_number(n):
return n * (n + 1) // 2
```