Python — knotty interview questions
practical examples
Throughout my software engineer career, I’ve come across some recurrent Python interview questions. I’ve compiled a list of the most interesting (practical/tricky) ones, from which I’ve learned on the spot, so without further ado:
- Given the following list
l = [3,4,6,1,2]
willl.sort()==sorted(l)
returnTrue
orFalse
?
The result of l.sort()==sorted(l)
will be False
because sort
function will sort the list in-place and return None
, while sorted
will return a new list containing all items from the iterable in ascending order.
- Declaring dictionary
d
as such, how is going to be the structure of the dictionary?
d = {True:'yes',1:'no',1.0:'maybe'}
print(d)
d
will have the following structure {True:'maybe'}
, basically will contain the first key and the last value, because the dictionary keys must be unique, and in this specific case, keys are evaluated as equal behind the scenes True==1==1.0
. The interpreter creates a new dictionary and assigns the key:value in the order given by the dict expression.
- What will be the output of the following snippet (filter with list comprehension)?
even_no = list(filter(lambda x:x%2,range(10)))
At a first glance, one might be tempted to say it’s list comprehension which will contain all even numbers until 10. Actually, the list will contain the odd numbers [1, 3, 5, 7, 9]. Thefilter
which will construct an iterator for those elements from the iterable for which the function (lambda, in this case) returns True
), used in conjunction with the%
modulo operator (which returns the remainder of the division), behind the scenes, will have something like bool(0%2)
this will be False
and therefore filtered.
We can fix the list, in order to have the odd numbers [0, 2, 4, 6, 8], like this:
even_no = list(filter(lambda x:x%2==0,range(10)))
- What will be the output of the following snippet (closure)?
def multipliers():
return [lambda x: i * x for i in range(5)]print([m(2) for m in multipliers()])
The output will be [8, 8, 8, 8, 8 ], because python closures are late-binding, which means that the values of variables used in closures are looked up at the time the inner function is called.
A more in-depth answer can be found on StackOverflow under this question.
- What is the difference between the following expressions
a+=b
anda=a+b
?
The two expressions are not the same, you can try it with an iterable.
l=[1,2,3]
ll=l
ll+=l # ll == [1, 2, 3, 1, 2, 3]and l == [1, 2, 3, 1, 2, 3]
ll is l # will return True
Whereas
l=[1,2,3]
ll=l
ll = ll + l # ll == [1, 2, 3, 1, 2, 3] and l == [1,2,3]
ll is l # will return False
The difference is that the first expression extends and the second expression creates a new object, usually, the python interpreter will check if an object implements the __iadd__
operation (+=
) and only if it doesn't it will emulate it by doing an add operation followed by an assignment.
Another example of this behavior can be:
a=[1,2]
b="three"a+b # will throw TypeError: can only concatenate list (not "str") to lista+=b # will modify a as such [1, 2, 't', 'h', 'r', 'e', 'e']
In I would dare to say that you’ll encounter one of those questions, in a more or less similar form, ultimately the Zen of Python…