Skip to content

Instantly share code, notes, and snippets.

@decorator-factory
Last active January 16, 2023 13:27
Show Gist options
  • Save decorator-factory/e8f08fd5fc997e45d4b52dfb2b391325 to your computer and use it in GitHub Desktop.
Save decorator-factory/e8f08fd5fc997e45d4b52dfb2b391325 to your computer and use it in GitHub Desktop.
Silly interview questions and answers

When I was looking forward to my first job interview, I was (naturally) anxious, so I wanted to prepare well. I opened up a search engine and typed in: "Python interview questions".

To my surprise, I found dozens of articles with outdated, plain wrong and supernaturally hilarious "interview questions" and even worse "answers" to them. In this post, I've compiled some of them with my own commentary. I've sprinlked in some related guides of poor quality.

I tried ordering the sections as they lose the connection to reality.

Dynamic typing

Source

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

Dynamically typed– the developer does not assign data types to variables at the time of coding. It automatically gets assigned during execution.

It's true that Python is dynamically typed, but the definition of "dynamically typed" is wrong.

This is a valid, statically typed Haskell function - with zero type annotations:

greeting name = "Hello, " ++ name ++ "!"

And this is a valid, statically typed Rust function:

fn main() {
    let foo = 1;
    let foo = "bar";
}

(Rust lets you shadow a variable you're not using anymore - even if the new variable has a different type)

In a statically typed program, variables ("names", "symbols", etc.) have types. Sometimes you have do explicitly declare the type of a variable, sometimes it's inferred by the compiler. In Python, values have types, but names don't.

Having to declare the type is called "explicit typing". That's opposed to "implicit typing" or "type inference", which is supported by many modern statically typed languages (C++, Java, Kotlin, C#, F#, Rust, Haskell).

A common claim among these articles is that dynamic typing is a benefit of Python. I wouldn't be so certain: it's both a blessing and a curse.

Executed line by line

Source

  1. Interpreted language– Since python is interpreted language, it executes the code line by line and stops if an error occurs in any line.

No, Python doesn't execute source files line by line. Please stop saying that. "Interpreted" usually means "compiled to an intermediate form and executed in a virtual machine, and not compiled to machine code". "Interpreted"/"compiled" is a property of an implementation, not the language, anyway — take a look at Nuitka.

What is the difference between range & xrange?

The difference is that there's no xrange in Python 3, and Python 2 is dead. It would be a red flag for me if something like this was asked in a technical interview.

Source 1, Source 2

[range in Python 3/xrange in Python 2] returns a generator object as it doesn’t really generate a static list at the run time

Actually, range is a class. When you call range, you get a range object back. It's a sequence (and therefore it's iterable), but it's not an iterator (and definitely not a generator).

>>> r = range(3, 70, 5)
>>> r[0]
3
>>> r[10]
53
>>> r[7:]
range(38, 73, 5)
>>> next(r)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'range' object is not an iterator
>>>

What's the difference between a tuple and a list?

"Tuples are faster than lists"

Objects don't have speed, they move at precisely 0 km/h. However, different operations on different objects do execute at different speeds.

It is true that accessing an element of a tuple by index is slightly faster than in a list. But that might change if you pick a different implementation of Python (like PyPy) or just update your version of Python. For some other tasks, lists will lead to better performance - for example, a list comprehension is generally faster than calling tuple on a generator expression.

Some other differences between lists and tuples have been provided by Intellipaat:

  • Lists are less reliable in terms of errors as unexpected changes are more likely to occur
  • Tuples are more reliable as it is hard for any unexpected change to occur

Unexpected changes? Like cosmic rays? Perhaps they were trying to explain the advantages of immutability, but that's a whole other topic.

  • Lists consist of many built-in functions.
  • Tuples do not consist of any built-in functions.

...what?

Making a script executable

Source

Explain how can you make a Python Script executable on Unix? Script file must begin with #!/usr/bin/env python

Nope, that doesn't make it executable. See this SO answer for what #!/usr/bin/env does.

How to remove duplicate elements from a list?

Source 1, Source 2, Source 3

A common solution is to make a set from the list, and then to make a list from the resulting set. However, that's not a satisfactory answer for a real-world problem.

We're missing some details from the requirements: what elements are in the list? Are these elements hashable? Do we care about preserving order? How big is the list, and what is our time/memory budget?

Incorrect explanation of the GIL

Source

GIL stands for Global Interpreter Lock. This is a mutex used for limiting access to python objects and aids in effective thread synchronization by avoiding deadlocks. GIL helps in achieving multitasking (and not parallel computing). The following diagram represents how GIL works. Image Based on the above diagram, there are three threads. First Thread acquires the GIL first and starts the I/O execution. When the I/O operations are done, thread 1 releases the acquired GIL which is then taken up by the second thread. The process repeats and the GIL are used by different threads alternatively until the threads have completed their execution. The threads not having the GIL lock goes into the waiting state and resumes execution only when it acquires the lock.

The diagram is OK, but the explanation is completely backwards. What's the point of releasing the lock after the I/O operation is done? In reality, when a thread starts waiting for some I/O, it releases the GIL. This allows a different thread to do something in the meanwhile (for example do its own I/O). Several threads can wait for I/O at the same time.

For more information about the GIL and why it's needed, watch this talk by Larry Hastings.

What is pickling and unpickling?

Source

Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling.

The misleading part here is "any Python object". That is wrong: you can't pickle anonymous functions, file objects, locks, and many other things. See "What can be pickled and unpickled?".

What are the built-in types of python?

Depends on what you mean by "built-in type". Does this only include classes that are "built-ins" (i.e. names available without importing anything)? Does this include types from the standard library? Does this include, for example, the type of None (there's no "built-in" name for it, but it's accessible as type(None))?

Here are some (wrong) answers I've seen:

Source 1

  • Integers
  • Floating-point
  • Complex numbers
  • Strings
  • Boolean
  • Built-in functions

Source 2

Mutable built-in types:

  • Lists
  • Sets
  • Dictionaries

Immutable built-in types:

  • Strings
  • Tuples
  • Numbers

What is Polymorphism in Python?

I've seen this "answer" copied a bunch of times, but here's one of the sources.

69. What is Polymorphism in Python? Polymorphism is the ability of the code to take multiple forms. Let’s say, if the parent class has a method named XYZ then the child class can also have a method with the same name XYZ having its own variables and parameters.

No, that's not polymorphism. If a child class's method signature is incompatible with the parent's signature, that's a violation of LSP.

Polymorphism is the ability to work with different concrete types. In the object-oriented world it often means subtype polymorphism, but there are other kinds like parametric polymorphism.

Not the correct syntax?

These websites mean completely arbitrary things by "syntax". Example (source):

Which one of the following is not the correct syntax for creating a set in Python?

  1. set([[1,2],[3,4],[4,5]])
  2. set([1,2,2,3,4,5])
  3. {1,2,3,4}
  4. set((1,2,3,4)) Ans.

set([[1,2],[3,4],[4,5]])

Explanation: The argument given for the set must be iterable.

Well, set([[1,2],[3,4],[4,5]]) is syntactically valid. And the argument to set is iterable. The issue here is that the elements of the iterable (lists in this case) are not hashable.

"Type conversion"

Explicit type conversion is also known as typecasting.

Calling int, str, float etc. on a value is sometimes referred to "type conversion" or "type casting". I think that's not a very good way of explaining what calling these classes does, and some people incorrectly use terms from other languages.

stringly_number = "42"
number = int(stringly_number)

What happens in the above snippet is not really "type conversion". When you call int on a string, it attempts to parse the string and then create an int object if the string encodes a decimal integer.

This explanation I found is so bad that I dedicated a whole mini-rant to it:

Source

In Python, when the data type conversion takes place during compilation or during the run time, then it’s called an implicit data type conversion. Python handles the implicit data type conversion, so we don’t have to explicitly convert the data type into another data type. We have seen various examples of this kind of data type conversion throughout the tutorial. Don’t worry if you can’t recall it. Let’s see another example. Let’s add two Python variables of two different data types and store the result in a variable and see if the Python compiler is able to convert the data type of the resultant variable or not.

a = 5
b = 5.5
sum = a + b
print (sum)
print (type (sum)) #type() is used to display the datatype of a variable


Output:
10.5
<class ‘float’>

In the above example, we have taken two variables of integer and float data types and added them. Further, we have declared another variable named ‘sum’ and stored the result of the addition in it. When we checked the data type of the sum variable, we can see that the data type of the sum variable has been automatically converted into the float data type by the Python compiler. This is called implicit type conversion. The reason that the sum variable was converted into the float data type and not the integer data type is that if the compiler had converted it into the integer data type, then it would’ve had to remove the fractional part and that would’ve resulted in data loss. So, Python always converts smaller data type into larger data type to prevent the loss of data.

First: in Python, variables don't have types, objects have types! (again). There's no such thing as "a float variable" or "an int variable". Any variable can refer to any object whatsoever.

The "type conversion" done by the "Python compiler" in this article is completely imaginary. There is just no such thing. The reason why adding an int object and a float object works like this is that one of them (or both of them) handles this situation in its __add__ magic method.

class Apple:
    def __add__(self, other):
        if not isinstance(other, Orange):
            return NotImplemented
        return "Something completely different"

class Orange:
    pass

print(Apple() + Orange())  # output: Something completely different

Now that we know the in-built functions provided by Python that are used for explicit type conversion, let’s see the syntax for explicit type conversion: (required_data type)(expression)

No, that's not special syntax. That's just the syntax for a call.


I think it's sometimes necessary to simplify things for pedagogical purposes. But making up complex mechanisms like this does not help with education.

I like my decorators medium rare

Source

Q8.What are decorators in Python?

Ans: Decorators are used to add some design patterns to a function without changing its structure. Decorators generally are defined before the function they are enhancing. To apply a decorator we first define the decorator function. Then we write the function it is applied to and simply add the decorator function above the function it has to be applied to. For this, we use the @ symbol before the decorator.

— Sir, would you like to add some design patterns to your function?

— Yes please, what kind of patterns do you offer?

— We offer creational, behavioural, and structural patterns!

— Then I would like 100 grams of Strategy and a couple Iterators. But please none of the structural stuff, I've heard they change my function's structure. My uncle's helper function is still recovering from a misplaced Adapter.

A collection from hackr.io

Source

Question: Do runtime errors exist in Python? Give an example? Answer: Yes, runtime errors exist in Python. For example, if you are duck typing and things look like a duck, then it is considered as a duck even if that is just a flag or stamp or any other thing. The code, in this case, would be A Run-time error. For example, Print “Hackr io”, then the runtime error would be the missing parenthesis that is required by print ( ).

I don't know what they were trying to say here. First they mentioned duck typing (is using duck typing a runtime error?), then they mentioned the only error in Python that's not a runtime error - a syntax error.

Question: Does Python support an intrinsic do-while loop? Answer: No Python does not support an intrinsic do-while loop.

Asking questions about other languages does have certain value. If a programmer is well-rounded, they might tell you about different paradigms or how different technologies compare to each other. But this kind of question is just bad. (One common question is about "access specifiers". Well, guess what, they are different in different languages. Why do you want me to tell you about access specifiers in Java?)

Question: What is, not and in operators? Answer: Operators are functions that take two or more values and returns the corresponding result.

  • is: returns true when two operands are true
  • not: returns inverse of a boolean value
  • in: checks if some element is present in some sequence.

How do you manage to be wrong in every sentence? (Never mind the grammatical puzzle which is the question)

operators are functions

Not really. An operator is a syntactic element, and a function is an object.

that take two or more values

not takes just one value. Comparison operators (< > <= >= == != is is not in not in) can be chained, but that's not the same as "taking more than two values".

and returns the corresponding result

It would be strange if a function returned an unrelated result :-)

  • is: returns true when two operands are true

I think you confused it with and.

  • not: returns inverse of a boolean value

That's partially true. But it also works with non-boolean values.

  • in: checks if some element is present in some sequence.

s/sequence/iterable

Is Python a case sensitive language?

Why does every single one of these sites include this question?

Suddenly Java?

In the middle of their big Python interview question list, InterviewBit decided to sprinkle in some Java questions!

45. Differentiate between new and override modifiers. The new modifier is used to instruct the compiler to use the new implementation and not the base class function. The Override modifier is useful for overriding a base class function inside the child class.

46. Why is finalize used? Finalize method is used for freeing up the unmanaged resources and clean up before the garbage collection method is invoked. This helps in performing memory management tasks.

Nobody expects the Spanish inquisition!

Getting basic syntax wrong

Source

Example of dictionary comprehension is- x=[i : i+2 for i in range(5)] The above code creates a list as below- [0: 2, 1: 3, 2: 4, 3: 5, 4: 6]

No comments. They live on a different planet.

How many angels can dance on the head of a pin?

Source

Q13.What are Keywords in Python? Ans: Keywords in python are reserved words that have special meaning.They are generally used to define type of variables. Keywords cannot be used for variable or function names. There are following 33 keywords in python-

  • And
  • Or
  • Not
  • If
  • Elif ...

First, this is just a bad trivia question. It doesn't display any real knowledge of Python. The correct answer is: just read the appropriate section of the docs.

They are generally used to define type of variables

I don't know what language that person is coming from, but I don't know a language where reserved words are primarily type names.

Also, why are the keywords capitalized?

Inventing new data types

Q14. What are Literals in Python and explain about different Literals Ans: A literal in python source code represents a fixed value for primitive data types. There are 5 types of literals in python- <...>

  • A character literal– It is created by assigning a single character enclosed in double quotes. Eg. a=’t’

First, there are no "primitive data types" in Python. That's just not a thing. They also don't need to represent a fixed value: a list literal can contain any expressions inside of it.

Second, there is no separate character type in Python. And you didn't even type double quotes correctly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment