Python Phone Interview Questions

A couple of weeks ago I did a written homework assignment with some essential Python interview questions. During the phone interview discussion after turning in the project, the lead Python Developer couldn't find anything to nitpick and even confessed me he thought I was a better Python Developer than him. The conversation went very well and I felt confident I would be invited to in person interview. You may already guess would happened next. I never heard from them again and all of my attempts at communication were ignored. I'd like to say this was an isolated incident, but that would be a complete lie. I just spent last weekend doing another Python homework assignment and I haven’t heard back from them yet.

Python Phone Interview Questions

Not every developer has the luxury of that much free time to focus on puzzle type Python interview questions just to get a foot in the door. All companies want you coding, sometimes over the phone, sometimes on a computer, sometimes on a white board. The companies take that too far and I am 100% positive that the daily job for people who work there does not involve reversing a string in place or writing a function that determines if a word is a palindrome or not, but those interview questions are meant to shrink the pool of potential hires from the thousands to the dozens. Nobody wants to waste time through a normal interview process with thousands of people, better to stump most of them with idiotic, useless quiz questions and interview only a few dozen developers in person. I get it.

Python Virtual Environment as an interview question

For the last few weeks I have been interviewing several people for Python developer position and to my surprise found out that all of them had no idea about Python virtual environments. Answering this common Python interview question may not get the job, but at least you would continue the interview process.

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project A depends on version Django 1.8 but, Project B needs Django 1.9.6” dilemma, and keeps your global site-packages directory clean and manageable.

$ pip install virtualenv

My disappointment is compounded when a candidate misses this topic even when they claim several years of Python experience. Isolation and repeatable clean environment development without hidden dependencies on the base system is a good thing. Virtual Environment primary goal is to conveniently provide Python level isolation. For Python packages that depend on system libraries, only the Python level part of those packages are isolated. Provided the
developer is fully conscious that this is python-level-only isolation, then it is useful.

I can sense some developers bristle at the mention of virtual environment. What about using Docker instead of virtual environment? You can install all dependencies inside a container instead of virtualenv. Installing dependencies in Docker has it's advantages over using just virtual environment, because thanks to containers, the developer doesn't have to worry about system's dependencies, and your app is now better isolated. However running Python becomes a bit cumbersome, because developer doesn't have dependencies installed on machine, and developer would have to get into docker container to run Python there. Just use Python as you always do

Python Essential Interview Questions

One of the most essential Python interview questions is explaining to interviewer the reason for having __main__() function in Python

if __name__ == "__main__":

Python does not have a well defined entry point like Java or C++, it simply executes a source file line by line. In Python, execution of code does not have to start at main. The first line of executable code is executed first. Every module has a name and statements in a module can find out the name of its module. This is partuculary useful in one particular situation, as mentioned before, when a module is imported for the first time, the main block in that module is executed. What if we want to execute the block only if the program was used by itself and not when it was imported from another module? This can be achieved using the name attribute of the module

ImportError: No module named - SOLVED

Before software developer goes in for a Python interview, there are a few things which one should refresh. For example how about solving "ImportError: No module named requests" error.

Imagine that the Python interpreter returns this error on basic "import requests" command

➜ ~ python
Python 2.7.11 (default, Jan 15 2016, 21:24:19)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "", line 1, in 
ImportError: No module named requests

The most obvious check is to run Python's package manager - pip to show details about an installed package. If it returns nothing, it means that package is not installed.

➜ ~ pip show requests
➜  ~

One more precise check is to execute the same pip to list installed packages

➜ ~ pip list
pbr (1.8.1)
pip (8.1.1)
setuptools (20.2.2)
six (1.10.0)
stevedore (1.12.0)
virtualenv (15.0.0)
virtualenv-clone (0.2.6)
virtualenvwrapper (4.7.1)
wheel (0.29.0)

The usual trick to answer this Python interview question is to install the request package by running the simple command and than check the installation again.

➜ ~ pip install requests

Free PyCharm license key

How to get a free PyCharm license key? JetBrains PyCharm and many other professional developer tools from JetBrains like WebStorm, PhpStorm and IntelliJ IDEA, are free for students with a valid .edu address or or a valid ISIC card. It also works for students outside of the US! Keep in mind that students could use JetBrains products for non-commercial, educational purposes only, including conducting academic research or providing educational services.

In my opinion PyCharm is legitimately the best Python IDE that I have encountered. Some of my favorite features of tool include

  • top-notch code editor
  • good syntax highlighting and code completion, including for non-Python languages
  • built-in inspections that you can adjust to your liking
  • automated, smart refactoring
  • handy interfaces for debugging, build management and deployment and database management
  • integration with popular frameworks like Django, Flask, Google App Engine, Pyramid, and so on.
  • error highlighting (like it as a beginner !)
  • you can navigate to a function or class declaration/implementation in one keystroke
  • unit testing integration

There are a lot of others but those are some of the big ones I can dream of. Of course as many other Python IDE, PyCharm tends to consume RAM and CPU, so slower computers will struggle with JetBrains PyCharm comparing with text editors like Sublime Text 3. As many other Python beginners notice - with amazing plugin support and superb Python tools integration, PyCharm became the best Python IDEs out there.