Python Job Interview: Questions That Will Trip You Up

As an ‘engineer’ in an investment bank, I get to conduct a lot of interviews, especially with people who claim to be able to code in Python. They might be fellow coders, but they can also be traders who say they know how to code. After all, Python is a language with plenty of newcomers, and plenty of people making big claims about their Python proficiency… sometimes incorrectly.

Before you step into a room claiming you know Python, you need to familiarize yourself with the following stumbling blocks from a typical Python job interview. I’ve tripped up a lot of interviewees with these myself; this is my guide on how to remain standing. 

“What’s a list comprehension?”

On the face of it, this looks like an easy question to answer. It’s a shorthand way of creating a new list; for example, adding two to every number in an existing list:

new_list = [ x + 2 for x in numbers ]

But then the interviewer might delve a bit deeper, and ask something like: “So it’s the equivalent of a for-loop appending to list?” A naive candidate would say “yes,” but that’s not true (usually). List comprehensions are more efficient than loops, where the list in the loop is created with no elements initialized.

In the list comprehension, we know what the size of the result is going to be; therefore, Python can allocate the memory up-front, which is much more efficient than dynamically adding to a list.

The interviewer might get even more evil by asking whether defining lambda expressions directly in a list comprehension is a good idea. It is not. So, a simple question can quickly get bogged down in minutiae about things you don’t consciously think about day-to-day, even as an experienced developer.

This is a real challenge as a new coder: It’s natural for interviewers to see how deep a candidate’s knowledge goes. My advice is to be up-front and not get flustered when you don’t know something. That way, the interview can quickly move on to something you might know.

“Why do people say that Python is single-threaded?”

In this instance, the interviewer wants to know your understanding of the global interpreter lock. Python has thread packages; but in some versions of Python, the global interpreter lock ensures that, at any given point in time, only one thread can execute (it holds the lock). It might look like things are running in parallel, but they’re really not. It increases the speed of single threaded applications and makes integration of C libraries (which aren’t thread-safe) easy. So far, so good.

The interviewer then might ask: “Can we run anything in parallel in Python?” This is when they want you to talk about processes versus threads, and the fact that Python has a good multiprocessing library. We could also farm out requests to a compute pool or a job queue such as Celery. The global interpreter lock also doesn’t stop threads processing many input/output requests at the same time; the lock is shared, whilst threads are waiting for the requests to finish (this is particularly effective on versions of Python after 3.2).

Don’t Lose Your Head

As a new coder in a trivia-style interview, it’s very likely that you won’t know everything that will be asked; the trick is to deal with that.

It goes without saying that you should know the major syntactic elements of Python: Things like generators, the differences between list and tuple, lambda expressions, filtering iterables, and so on.

It’s very helpful to know how the core data structures work. For example, you’ll often be asked how a dictionary works under the hood. You should also have opinions on the type system and interpreted nature of the language in comparison to a statically typed and compiled language (it’s a question of trade-offs). As in any other language, you should make sure to have practiced algorithm and data structure questions on a platform like HackerRank or Leetcode. You can also be presented with code on paper, and it’s useful to practice dry-running code in your head, as well.

James Jones is the pseudonym of a technologist in a U.S. investment bank. This article originally appeared on eFinancialCareers.