Preparation to Python Interview

Fri 02 November 2012

I decided to collect a little more information and experience during preparation to Python developer interview. These are some information and links which seemed important to me. Maybe it will be helpful.

How does it usually go?

What kind of projects did you participate in?

What did you do at your previous job? It is expected that you will told the essence in simple words.

Tricky question

It is expected that you will search for solution of task independently. Reasonings aloud are welcomed.

Writing code

Interviewer is interested in critical analysis of code. For example, efficiency of used data structures, algorithm's complexity evaluation.

Design

In this step it is important to ask as much as possible about a task before starting to look for solution.

Provocative question

You have to stay in your lane.

Questions

Basic

Probably interviewer starts with basic questions. Let us see example:

funcs = []
for i in range(5):
    def f():
        print i
    funcs.append(f)

for f in funcs:
    f()

I guess he examines knowledge about namespace. It will print 4 five times. To explain that you supposed to know about LEGB rule. Also you should know that variable search in enclosed scope will be done later, after call of enclosed functions. They all get same value -- value of i in last iteration.

Next example prints numbers from 0 to 4:

funcs = []
for i in range(5):
    def f(i=i):
        print i
    funcs.append(f)

for f in funcs:
    f()

It happens because default value is stored when enclosed function was created.

There is another popular question related with default value of function. N.B. names is mutable:

def f(names=[]):
    names.append('some name')

Visualizing code execution and quiz of non-trivial features of Python will be helpful to prepare for questions which are mentioned above.

What do you think about this code?

d = {1: 'a', 2: 'b', 3: 'c'}


def safe_get(d, key, value):
    if key in d.keys():
        return d[key]
    return value

print safe_get(d, 0, 'blah')

Apparently interviewer expects these answers:

  • it repeats functionality of d.get(0, 'blah').
  • it uses if key in d.keys() instead of if key in d.
  • actually it is looking for key in [1, 2, 3] (for Python 2). Therefore O(n) is worse than O(1) for dictionary lookup.

It would be appropriate to read about data structures and their time complexity, about collections, itertools and sorting.

Category: Python Tagged: python interview

Comments