Core Python Concepts
- Data types and mutability — knowing which types are mutable vs immutable
- Memory management — how Python handles references, garbage collection, and the GIL
- Functional features — decorators, generators, closures, and higher-order functions
Q1.What is the difference between a list and a tuple in Python? When would you use each?
Q2.Explain Python's GIL (Global Interpreter Lock). How does it affect multithreading?
multiprocessing module — separate processes, each with its own GIL
• Use libraries like NumPy that release the GIL during C-level computations
• Use concurrent.futures.ProcessPoolExecutor for a higher-level API
Recent developments:
• Python 3.13 introduced an experimental free-threaded mode (--disable-gil) that removes the GIL entirely, though it's not yet the defaultQ3.What are decorators in Python? Write a simple example.
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f'{func.__name__} took {time.time()-start:.2f}s')
return result
return wrapper
Usage: @timer above a function definition. Under the hood, @timer on def my_func is equivalent to my_func = timer(my_func).
Common real-world decorators:
• @functools.lru_cache — memoization
• @property — computed attributes
• @staticmethod — static methods
• Framework decorators like Flask's @app.route
Best practice: Always use @functools.wraps(func) in your wrapper to preserve the original function's metadata.Data Structures & Algorithms in Python
- Built-in data structures —
dict,set,list,deque, and their time complexities - The collections module —
Counter,defaultdict,OrderedDict,namedtuple - Pythonic patterns — list comprehensions, generator expressions, and
itertools
Q4.How does Python's dictionary work internally? What is its time complexity?
dict uses a hash table with open addressing (specifically, a probe sequence using perturbation).
How insertion works:
1. Python computes the key's hash
2. Maps it to a table index
3. Stores the key-value pair there
4. If two keys hash to the same index (collision), Python probes the next available slot using a perturbation-based sequence
Time complexity:
• Average-case: O(1) for get, set, and delete
• Worst-case (all keys collide): O(n), but Python's hash function makes this extremely unlikely
Key details:
• Since Python 3.7, dictionaries maintain insertion order (implementation detail in 3.6, guaranteed in 3.7+)
• Python over-allocates the hash table (resizing at 2/3 capacity) to keep collisions low, trading memory for speed
• Keys must be hashable — implementing __hash__ and __eq__ is required for custom objects used as keysPython OOP & Design Patterns
- Class mechanics —
__init__,__new__,__repr__, and the MRO (Method Resolution Order) - Inheritance patterns — mixins, abstract base classes, composition vs inheritance
- Design patterns — Singleton, Factory, Observer, and Strategy patterns in Pythonic style
Q5.Explain the difference between __init__ and __new__ in Python.
__new__ is the class constructor — it creates and returns a new instance of the class
• __init__ is the initializer — it sets up the instance after it's been created
Call sequence:
1. obj = MyClass.__new__(MyClass)
2. MyClass.__init__(obj)
When to override __new__:
In practice, you rarely override __new__. The main use cases are:
• Implementing singletons (return the same instance every time)
• Subclassing immutable types (str, int, tuple) — you must set the value in __new__ since the object is immutable by the time __init__ runs
• Metaclass programming
Important caveat: If __new__ doesn't return an instance of the class, __init__ won't be called.Frequently Asked Questions
Which Python version should I use in interviews?
Use Python 3.10+ features confidently — match statements, walrus operator (:=), f-strings, and type hints are all fair game and show modern Python knowledge. Avoid Python 2 syntax. If the interviewer's environment uses an older version, they'll tell you.
How important is knowing Python internals for interviews?
For data science and general SWE roles, understanding the GIL, dict internals, and memory model gives you an edge but isn't required. For senior backend or infrastructure roles, deep Python internals knowledge (garbage collection, C extensions, asyncio event loop) is expected.
Should I use built-in functions or implement from scratch in interviews?
Ask the interviewer. Generally, use built-in functions for the overall solution (sorted(), collections.Counter) but be prepared to explain how they work internally. If asked to implement a data structure, write it from scratch. Showing you know the standard library is a positive signal.
Don't freeze in your next interview
InterviewsUnlocked gives you real-time AI coaching during live interviews — role-tailored answers, follow-up cues, and confidence when you need it most.
Related Resources
Top Data Scientist Interview Questions & Answers
Prepare for data science interviews with expert questions on machine learning, statistics, Python, deep learning, and A/B testing with detailed model answers.
Read moreInterview QuestionsTop Software Engineer Interview Questions & Answers
Prepare for your software engineering interview with expert-crafted questions and detailed answers covering data structures, algorithms, system design, and behavioral topics.
Read moreSkills & TechnologiesSQL Interview Questions: From Basics to Advanced Queries
Master SQL interviews with questions covering joins, window functions, CTEs, query optimization, and real-world data analysis problems with expert solutions.
Read moreSkills & TechnologiesMachine Learning Interview Questions & Answers: Concepts to Practice
Comprehensive ML interview prep covering supervised and unsupervised learning, neural networks, feature engineering, model evaluation, and real-world problem-solving.
Read moreInterview TipsTechnical Interview Preparation: A 4-Week Plan
A structured 4-week technical interview preparation plan covering data structures, algorithms, system design, and mock interviews with daily schedules.
Read more