10 Python questions from a top hedge fund
If you're an interviewing for a Python coding job with a hedge fund, you might want check you can answer these questions first.
They were published last week by Man Group's quantitative technology team on Twitter. Although they're not interview questions explicitly (you're supposed to answer them for fun), they at least give you an idea of the level of coding expertise that might be expected of you. - The questions come from a quiz Man organized for its own coders at a virtual Python offsite.
If you can come up with the right answers all the way through, the implication is that you're as good a Python coder as anyone at Man. The questions are supposed to get harder and harder as you progress - although only 51% of online participants got question one correct, compared to 71% who solved question three accurately.
Python question one: What happens if you run this Python code?
"Stay positive." The conditional evaluates a non-empty tuple which is "truthy". Although each item in the tuple is "falsely" (False, 0 and an empty list) the tuple itself evaluates to True.
Python question two: What happens if you run this Python code?
Error. This raises an UnboundLocalError – “a” & “b” are local variables, b is referenced before assignment (when a is assigned to b). Fix this using the “global” or initialise “a” & “b” inside the function scope.
Python question three: What happens if you run this Python code?
2. “bool” is a subtype of “int” (for quirky historical reasons - bit.ly/2Xy1ipI) so summing the list of bools is equivalent to summing a list of ints.
Python question four: What happens if you run this Python code?
[1,2,3,4,5,6] The default arg, “my_list”, is mutable, so it isn't instantiated with every function call Calling the fn twice adds the other args to the same list instance each time Don't use mutable types as default args!
Python question five: What happens if you run this Python code?
“Still waiting…” Python does not have a decrement operator so line 4 has no effect. It just gets parsed as two negative signs in a row, i.e. the equivalent of “-(-seconds).”
Python question six: What happens if you run this Python code?
[10,10,10,10,10] The L1 fns are closures: they have access to a variable from the enclosing scope that has completed its execution (the list comprehension). Py closures are late binding: their value is looked up at the time the closure is called. By the time the lambda fns are called the list comprehension has executed and i==4 so all the lamba fns return 10. Fix the code in question 6 by writing: multipliers = [lambda x, i=i: (i+1)*x for i in range(5)]. This forces the early binding of “i” as a default argument in the lambda fns. This works due to the behaviour we saw in Q4 - default args are created once, at the time the fn is created, rather than when it is called.
Python question seven: What happens if you run this Python code?
[1,2] [1,2] The += operator on L3 uses the “__iadd__” method which modifies the list in place. L7 uses the “__add__” method which takes two parameters and returns their sum without modifying either.
Python question eight: What happens if you run this Python code?
12 1 123 1. The += operator only uses the __iadd__ method if it exists. Strings are immutable so do not have __iadd__ therefore lines 3 and 7 are equivalent.
Python question nine: What happens if you run this Python code?
Error. Tuples do not support item assignment so, while you can add an item to the list at position 0 you cannot then assign that list to be item 0 in the tuple.
Python question 10: What happens if you run this Python code?
True False True. “a” & “b” start off referencing the same int object. After L4 & L5 they both reference different int objects with the same value, so no longer have reference equality. Python keeps an array of ints between -5 & 256 When you create an int in that range you get a reference to a pre-existing object After L8&9, a& b reference the same object again!
Have a confidential story, tip, or comment you’d like to share? Contact: email@example.com in the first instance. Whatsapp/Signal/Telegram also available. Bear with us if you leave a comment at the bottom of this article: all our comments are moderated by human beings. Sometimes these humans might be asleep, or away from their desks, so it may take a while for your comment to appear. Eventually it will – unless it’s offensive or libelous (in which case it won’t.)