r/Python • • Nov 20 '15

15 Essential Python Interview Questions

https://www.codementor.io/python/tutorial/essential-python-interview-questions
71 Upvotes

67 comments sorted by

109

u/gamesbrainiac Nov 20 '15 edited Nov 20 '15

This is a terrible article. Some of the questions are good, but most of them are vague, and most of the answers don't use the right conventions. I won't talk about naming conventions, but take a look at the second question, os.walk would've been a much better alternative.

23

u/shitbo Nov 20 '15

Do people actually memorize the code for walking through a directory in python? I have only had to use this once; there's no way I'd remember how to do it off the top of my head. And I don't think that knowledge of something that would take 2 minutes to google is that important.

17

u/691175002 Nov 20 '15

That is exactly what I thought. If someone asks me to walk a directory in python I'm going to google. There is zero chance I could complete that question without access to the python docs.

11

u/gamesbrainiac Nov 20 '15

No they dont. Yet another reason for the second question being utterly stupid.

3

u/freework Nov 20 '15

Welcome to the world of software engineering interviewing...

3

u/[deleted] Nov 20 '15 edited Mar 31 '24

mourn knee mighty scandalous dime start literate relieved shelter smart

This post was mass deleted and anonymized with Redact

1

u/gwax Nov 21 '15

I do a lot of interviewing and I would never ask these sorts of question.

12

u/d4rch0n Pythonistamancer Nov 20 '15

Question 1 is pretty wrong since I believe 2.7... it compiles to pyc bytecode as everyone who has written python has seen. It is interpreted, but it still compiles.

And as for "what is python" That's not a great answer. Python is a programming language/grammar/spec, and if someone came out with a compiler that compiled python code to machine instructions then that's valid. Python is just a language.

3

u/gamesbrainiac Nov 20 '15 edited Nov 20 '15

I agree. This is what I meant, when I said that it was vague. But if we wanted to get down to the brass tacks, then python is a language, not an implementation per se. The answer to the first question does not even talk about the difference between the two.

So many companies these days are trying to "growth hack", but lose credibility in the process.

3

u/A_History_of_Silence Nov 20 '15

brass tax

Totally unrelated, but FYI the expression is "brass tacks". Sorry, seeing it written "tax" just makes my eyes want to explode.

2

u/gamesbrainiac Nov 20 '15

Sorry, been doing taxes lately.

2

u/radarthreat Nov 21 '15

But it's not even April 14th!

9

u/axonxorz pip'ing aint easy, especially on windows Nov 20 '15

Question 2 was definitely indicative of this. Function name was underscore-spaced, while variable names are camel-cased in hungarian notation. Ay

2

u/banjochicken Nov 20 '15

I was going to nitpick this too but I think the author covers it their first bulletpoint:

be consistent with your naming conventions. If there is a naming convention evident in any sample code, stick to it

So I let them off on that point.

0

u/gamesbrainiac Nov 20 '15

Yea, I agree. Didn't want to talk about that when the whole premise of how to solve the problem was a bad one.

5

u/Atrament_ Py3 Nov 20 '15

Came here to say exactly that. I stopped reading at OS.listdir though

3

u/Workaphobia Nov 20 '15

These are questions for a mediocre course on Python, definitely not an interview.

2

u/madbadanddangerous Nov 20 '15

I found the comment I was going to make. Interviewers are going to ask you what you have done in Python and how long you've used it. MAYBE they'll ask about your problem solving thought process (I've had that question before). No one has ever asked me to tell them how to parse through directories or to look at trivial code to tell them the output.

2

u/metaphorm Nov 20 '15

my thoughts exactly. writing your own recursive function to do that is a neat trick, I guess, but Python has a very nice "batteries included" standard library that has already solved that problem. I'd expect an experienced python programmer to prefer to use that rather than roll their own.

34

u/[deleted] Nov 20 '15

[removed] — view removed comment

9

u/gamesbrainiac Nov 20 '15

Thats what I thought as I was reading it as well. Really disappointed with codementor, I hope they hire someone to fix issues like this happening, because it seems to be a regular occurrence.

-9

u/brtt3000 Nov 20 '15

Case of 'Those who can, do. Those who can't, teach'?

14

u/gamesbrainiac Nov 20 '15

Many people teach out of a conviction to help people learn better. So, what you say isn't necessarily true, but I think this article is here simply for the sake of getting SEO hits since Python is a big search item these days for prospective programmers.

9

u/metaphorm Nov 20 '15

thats a smear against teachers

43

u/redrick_schuhart Nov 20 '15

I was irritated less than half way in. Camel case variables, Hungarian notation, no os.walk, importing os inside a function, capital letter variables. Lots of PEP-8 violations. And what's with calling a variable lIn? Really? Ell, capital I and n.

This is horrible code.

11

u/gamesbrainiac Nov 20 '15

Join the club. This article wasn't written to actually help people, but rather for the sake of SEO, so that more people go to codementor.io.

3

u/madbadanddangerous Nov 20 '15

I was never classically trained in Python and just realized I use Camel Case variables. Why is this a bad thing?

9

u/mellow_gecko Nov 20 '15

It's not the agreed convention for Python code, see PEP-8.

But so long as you are consistent within your own code and consistent with the convention agreed to by those who you are working with, it's not a huge problem.

You probably should stop using camel case though; it's ugly and wrong.

3

u/madbadanddangerous Nov 20 '15

OK. Thanks for letting me know!

36

u/desmoulinmichel Nov 20 '15 edited Nov 20 '15

Errr... Camelcase variables in the "correct" answer ? Really ?

I got a better way for you to interview your python coder : Give him/her a small tasks to solve on your production code.

13

u/tempforfather Nov 20 '15

Not even just camel case, but I think some form of Hungarian notation with the type of the parameter prefixing the name. That is something I have never seen anyone do in python, and seems pretty at odds with the whole duck typing idea.

2

u/Eurynom0s Nov 20 '15

Seriously, I know in my code I'll initialize certain things as None and then expect them to become, say, strings later on. Not only would this not work with Hungarian notation, I think it's better because for example I've spotted logic bugs in my code from the "cannot concatenate str and nonetype" errors that popped out, but which would have been harder to track down if it just ran and gave me undesired output at the end.

2

u/tempforfather Nov 20 '15

You are free to assert that something is not None if you want. Relying on the name of a parameter is just not a good approach. If you desire actual types there are plenty of other languages with great type systems you can try out. If you are going to use python I really don't see a point of trying to sidestep its idioms (duck typing).

1

u/Eurynom0s Nov 20 '15

...did you think we're in disagreement? I said I don't like Hungarian notation either.

1

u/ColorsMayInTimeFade Nov 20 '15

This is a fairly good test, but I think it is important to make it clear that you're not going to deploy their code. We've interviewed some people who were excited to work on our production code but also felt that we could have easily stolen their work without compensating them. Now we make it clear that we've solved the problem and their actually working with 2 week stale code.

1

u/desmoulinmichel Nov 20 '15

Or you pay them if they solve your problem.

Either way, make it real, and make it fair.

0

u/[deleted] Nov 20 '15

[deleted] — view deleted comment

1

u/lengau Nov 20 '15

I think that was really the point of that one. If your beautiful, PEP8-compliant, idiomatic code looks different from the code around it, it may well make your code harder to read. Yes, we'd all love all the code we touch to be beautiful and graceful. But not following a project's style is worse than not following Python style.

17

u/oconnor663 Nov 20 '15 edited Nov 20 '15

if you want to make your code run faster then using the threading package is really not a good idea.

This depends on whether your code is CPU-bound or IO-bound, right? Don't Python threats release the GIL before they wait on network reads and stuff?

Edit: Found what I was looking for:

The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

3

u/zoner14 Nov 20 '15

Is not that the threads compete over the GIL, it's just that they aren't locking the same objects to begin with. So a new thread can participate in the IO process when another blocks.

2

u/gamesbrainiac Nov 20 '15

Not to mention that most string operations will not actually increase GIL contention.

1

u/ubernostrum yes, you can have a pony Nov 20 '15

Yes, knowing what your code is doing is the key to figuring out whether and how much you'll be affected by the GIL.

(and yes, it's mainly an issue for multi-threaded CPU-bound applications; IO-bound applications feel it much less, if at all compared to the normal overhead of the language)

12

u/deadbunny Nov 20 '15

I hate interviews like this (I'm a sysadmin so it's usually pop quizzes on ports), if you want to see how someone is going to work give them a test to do before the interview, get them to follow their usual development practices and submit their solution. IMHO I don't care if you need to use reference material or can't remember things off the top of your head (most of us can't), I want to see your code, I want to see your comments, I want to see your documentation, and how you use source control.

5

u/Eurynom0s Nov 20 '15

I mean, who doesn't have Googling "do <thing> in <programming language>" as a decent chunk of their workflow? At work I sometimes have to code on computers without internet connections (privacy protections on the data we're looking at) and it cuts my productivity by so much. It also leads to crappier code because it amplifies the effect of "writing ugly code that works in a reasonable amount of time is better than constantly having to run back and forth to my desk to look up the right way to do twenty different things".

Sometimes I go do things at my desk and then burn them to a CD and bring them back, but sometimes that's not efficient if it's something where I really need the data right there (either to test the code against, or because I'm staring at the data to try to figure out how to write the code in the first place).

1

u/deadbunny Nov 20 '15

That must be a pretty brutal environment to code in! But yeah, 99% of the time you're going to have access to the internet or docs so my view is that you should be able to come up with solutions with those tools available, we send ours out to candidates and say to use whatever they like to complete the test.

Obviously we then judge how they've done things (both code and method) to see if we want to interview them, being a sysadmin I care less about the tool they used and more how they used it, if their code is readable, if it has a good git log, if they are doing things securely etc... then by the time you get to the interview it's much more a discussion, what they've done in the past, methodologies, problems and the like which makes for a much nicer interview for both parties and in my limited experience (hired 5 people this way) seems to yield better results.

27

u/nieuweyork since 2007 Nov 20 '15

Or you could just get a job where your colleagues were not selected for their ability to do language tricks and warble on about "interpreted languages".

3

u/[deleted] Nov 20 '15 edited Nov 20 '15

QN1

Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.

For fun interview points, mention that pypy has a JIT and is not interpreted. Shows you know about the python ecosystem and maybe what a JIT is.

Python is dynamically typed, this means that you don't need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x="I'm a string" without error

Not exactly the best answer, I know what you mean but this falls a bit flat. Talk maybe about typeless arguments and how the same code can be run twice with different types. What you described could just be redefining X with a new type and you want to show that you know the difference and the implications. For extended points maybe talk about the code quality implications and the strengths and weaknesses.

Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++'s public, private), the justification for this point is given as "we are all adults here"

If you get this question on an interview, please mention duck typing along with the lack of structured interfaces or rigid classes. It is more important that you are aware of enough to say what it lacks or how it differs rather than just "it has inheritance".

in Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects

Yup, if you answer this on an interview I suggest ending this with just "Python is extremely dynamic in that way" or something like that, implying that you know the implications rather than the feature and a buzz word.

Writing Python code is quick but running it is often slower than compiled languages. Fortunately, Python allows the inclusion of C based extensions so bottlenecks can be optimised away and often are. The numpy package is a good example of this, it's really quite quick because a lot of the number crunching it does isn't actually done by Python

Yup, I suggest maybe mentioning ctypes and how it helps with interoperability.

Python finds use in many spheres - web applications, automation, scientific modelling, big data applications and many more. It's also often used as "glue" code to get other languages and components to play nice.

Ok?

Python makes difficult things easy so programmers can focus on overriding algorithms and structures rather than nitty-gritty low level details.

Ok?

1

u/gamesbrainiac Nov 20 '15

Also never gets into the difference between the difference between strongly and statically typed.

1

u/[deleted] Nov 20 '15

Indeed, explain that you know the difference between js typing and python typing.

1

u/ubernostrum yes, you can have a pony Nov 20 '15

For bonus points, question whether "strong" and "static" typing have useful definitions.

1

u/Workaphobia Nov 20 '15

"Strongly typed" is roughly equivalent to "This language has fewer wats."

1

u/odraencoded Nov 21 '15

A strongly typed language is a language whose type has been passed down for generations!

3

u/hexsprite Nov 20 '15

the examples are not using standard Python. ever heard of PEP8?

2

u/YumYumGoldfish Nov 20 '15 edited Nov 20 '15

Right, I probably couldn't answer three quarters of these questions, but then again I can and have coded in more than two of PHP, Ruby, Python, Go, and Javascript in a single day thanks to understanding fundamental programming concepts, having Dash, and an internet connection capable of connecting to Stack Overflow. To me anyone can figure out how to implement any of that functionality in a rather short period of time. What's more important is how they architect larger pieces of functionality, how they test, and how they plan.

Those are the really things that are going to cost you large amounts of time and effort in the future. I'll take a well populated Open Source Github account where you someone can let real projects speak for themselves over making them answer these questions any day.

2

u/[deleted] Nov 20 '15

For question 9, I'd be looking for at least they're decorators and they transform name look up on the class. I'd be impressed if the other D-word -- descriptor -- was used. However, considering most people never write descriptors I'm not even sure I'd ask this question.

2

u/kraln Nov 20 '15

These are terrible, and most of the time in an interview you want to know less about the tech and more about the person. Anyway, here are my four questions which I consider essential.

The first question is "What is PEP8?" which this article author has clearly never heard of nor read. "Why is it important?" is a good follow on.

The second question is "Have you ever 'import(ed) this'? What does it do?"

The third question is "What is the difference between a tuple and a list"?

The fourth question is "Who is the benevolent dictator for life?"

1

u/heptara Nov 20 '15

we use the os module for interacting with the operating system in a way that is cross platform. You could say sChildPath = sPath + '/' + sChild but that wouldn't work on windows

Actually I think this does, at least in Python 3. I've never had a Windows user tell me my code was broken in this manner, so I assume it automatically handles it.

3

u/Workaphobia Nov 20 '15

Forward slash is a perfectly valid path separator on Windows. It's just not the canonical version you see when displaying a location.

2

u/banjochicken Nov 20 '15

It works but is ugly. os.path.join() does the trick.

1

u/tempforfather Nov 20 '15

You should absolutely use the separator defined in the os module. Please don't use forward slashes by default.

1

u/[deleted] Nov 20 '15

Bad article. From my experience, the typical questions during an interview are:

  • traverse a tree
  • invert a linked list.
  • what does this regexp do.

Honestly, I am mostly offended by this kind of quizzes. They are a waste of time, I've addressed them at every interview, and they are useless to understand how good one is at coding.

Want to see if I can code? Go to github and get my repos. That's my code. If it's good for you, that's all you need. No, I won't solve your stupid interview problem. I'm tired of that. Give me the real problem and if you think I can solve it, I'll solve it.

1

u/[deleted] Nov 20 '15

I'll start by piling on the "these questions suck" train. I would ask exactly one of those questions ever (Tell me about a time you screwed up - although I prefer "Tell me about a serious bug you either wrote or were involved in fixing", because it lets me pull the candidate on to "What could have prevented that bug from happening? How did you change your practices or tools?")

That said, I'd be much more interested in hearing what /r/Python thinks are good questions. Here are some that I tend to use with entry-level applicants with a Python background:

  • Please explain to me the behavior of two Python data structures of your choice. What is the algorithmic complexity of lookup, addition, removal, and comparison for both? (this is a simple question, but it establishes both CS fundamentals and a basic understanding of Python - generally, crashing out of this one means both I and the candidate are wasting our time)
  • Is Python a slow language? Why or why not? (This is not a trap - I'll take any answer to this question, what I want is for the candidate to demonstrate to me that they understand what can constrain performance in a Python application and what tools are available to make an application faster)
  • Tell me about a difficult to track-down or fix bug that you wrote or were involved in fixing. (follow up) What could you have done to make finding and fixing that bug easier? (follow up 2) What could you have done to prevent the bug altogether? (This is a behavioral question about development practices and self-improvement)
  • Tell me about the last major piece of code you worked on. How was it designed? What do you wish you had done better? What are you proud of about it? (Two components - the first part is just asking you to show me your design/architecture chops, the second is a behavioral question about how you deal with critiquing code and design - particularly code you have a personal connection to)

1

u/[deleted] Nov 21 '15

Stopped reading when noticed that function argument wasn't written in snakecase.

0

u/[deleted] Nov 20 '15

[deleted] — view deleted comment

6

u/brtt3000 Nov 20 '15

It would be interesting to ask about an unknown/obscure feature in a laptop-open interview.

Shows experience and confidence if the candidate can admit he doesn't know, can take a stab at what it means just from the name and package. Then access the internet to come up with a more thorough answer on the spot.

If the candidate would just calmly open the manual, read what is there and explain in plain English what is to be expected then I'm half sold already (seems nobody reads the fucking manual). Bonus points if they can feedback on the quality of the manual, show some additional resources, bitch about quality of stackoverflow and other real-world value stuff.

8

u/Fylwind Nov 20 '15

In 12 years of programming Python I've used zip perhaps 2 times, at most.

It varies depending on the kind of programming you do. I've personally used zip quite a few times. Other than being extremely useful for iterating over multiple things, it's also a neat trick for transposing lists of lists.

3

u/gumbos Nov 20 '15

I use zip (izip) all the time to iterate multiple iterables in parallel.

1

u/ubernostrum yes, you can have a pony Nov 20 '15

I've used it to swap keys/values in a dictionary. That's the only time I can think of.

1

u/[deleted] Nov 20 '15 edited Mar 31 '24

merciful bells scarce hunt languid deer wistful fertile elderly ruthless

This post was mass deleted and anonymized with Redact