If you want a job working with Python, get ready for these classic interview questions...
The Global Interpreter Lock will always come up at some point.
CPython (the standard default official interpreter) is not thread-safe when it comes to memory management. So, if you're running multiple threads, the GIL is a bottleneck; it only allows one thread to access memory at a time. If everything is happening in one thread, then you're fine. But if multi-threading then, when one thread accesses memory, the GIL blocks all the other threads. This is a problem for multi-threaded python programs. It is not a problem for multi-processing python since each process has it's own memory.
Key words to say are "bottleneck", "multi-threading" and "memory management".
Solutions are to use multi-processing, use extensions written in C, or use other Python implementations like IronPython, or Cython.
In seven years of (web-centric) Python programming I've used multi-processing but never multi-threading. Nonetheless I always get asked this question in interviews.
An immutable type cannot be changed. Examples are integers, floats, strings, tuples. A mutable type can change. Examples are lists, dictionaries, sets, classes.
If you reel this off with ease they should dig deeper and try to expose your understanding of how immutable types point to actual values, whereas immutable types are pointers to locations in memory so, obviously, are subject to change.
This is actually a reasonable question. Mostly impractical but a little unique to python so anyone professing to be a python guru needs to understand it.
An iterator has the whole sequence in memory before it returns the first result. An iterator uses the "return". A generator calculates each result in the moment it is called for. The next result is unknown. A generator uses "yield".
A list comprehension is an iteration. eg: [x for x in range(1, 100)]
A generator expression is a generator. eg: (x for x in range(1, 100))
**that's a perfect example because you can go on to explain the range function is an iterator in python 2 and a
generator in python 3. By this point the interviewer will be already seeking another way to trip you up but, if they're
onto you, you will look stupid if you don't know anything about the xrange
function.
A single underscore is private. eg: _foo is not intended for access from outside the current scope. A double underscore is a reserved language construct. eg: __init__ is a python constructor, and __iter__ is the python iterator.
Note that the single underscore is only a convention; and not a rule enforced by the the language interpreter. You can access these variables/methods if you want to. The single underscore only indicates that you weren't supposed to do that.
Local is restricted to the function or class. Enclosed, like a javascript closure, exposes names to functions inside this function, but not functions outside it. Global means you can use it anywhere in the module/script.
In practice, scope is a non-issue in python, so interviewers can find it very useful to frustrate and fluster candidates into subordination and insecurity.
A lambda is a shortcut way to create a function. The key words to say are "anonymous function".
For extra credit, you might be asked to create a recursive function with a lambda. Surprisingly enough you can actually refer to the name you're declaring. eg:
factorial = lambda n: n * factorial(n-1) if n != 0 else 1
These questions, and my answers, for an engineering role associated with google might be interesting, and my answers, since you've come this far.
Plus it wouldn't hurt to be ready for these general classics:
I’m excited by the possibilities presented by your company. I’d like to be a part of its future success, and I want to help it to continue to grow by…
This role offers exactly the types of challenges and responsibilities I’m looking for and is ideally suited to my skills and experience…
This job is perfectly matched to my core competencies. I really feel like your company is the ideal fit for my interests, qualifications and passions...
My years of experience have given me the organisational / teamwork / sales / managerial skills to make me an invaluable employee for this company…
I found myself a little ‘stuck’ and decided I needed new challenges that weren’t readily available in my previous role…
I’m interested in a job with greater responsibility, and feel that a role like this would better offer those kinds of opportunities...
I was laid off due to a company restructure...
I’m organised, efficient and take great pride in doing the best work possible, and exceeding expectations…
My previous employer often commented on my ability to motivate and manage my team members – and I was even commended on my abilities with an industry award…
I know that public speaking is the number one phobia for most people, and I’d say that’s the main area I’d like to work on, especially as I know it’s part of the job description for this role...
I’m very detail oriented and meticulous, which means that I can sometimes take a little longer to get a task done, but I’m working on getting the right balance between attending to the detail and being as efficient as possible…