Unicorn Platform

World's best and simplest website builder powered by AI

Build your website
Saturday, January 13, 2024

5 advanced Python interview questions (explained on real world examples)

Question 1. How does Python handle memory management and garbage collection?

Python handles memory allocation automatically through its built-in memory manager. The memory manager allocates heap space for Python objects. Python also has an inbuilt garbage collector, which recycles all the unused memory so that it can be made available for the heap space. Here are five examples demonstrating various aspects of memory management and garbage collection in Python:

Example 1: Basic Memory Allocation

pythonCopy codea = 10 # Allocates memory for an integer object b = [1, 2, 3] # Allocates memory for a list object

Example 2: Reference Counting

`pythonCopy codeimport sys

x = 37 print(sys.getrefcount(x)) # Shows the reference count for the object 37 y = x print(sys.getrefcount(x)) # Reference count increases as x is assigned to y`

Example 3: Garbage Collection

`pythonCopy codeimport gc

print(gc.isenabled()) # Check if garbage collection is enabled gc.collect() # Explicitly run garbage collection`

Example 4: Cyclic References

pythonCopy codea = [] b = [a] a.append(b) # Creates a cyclic reference (a references b and b references a) del a del b gc.collect() # Garbage collector can handle cyclic references

Example 5: Using Weak References

`pythonCopy codeimport weakref

class MyObject: def my_method(self): print('My Method Called')

obj = MyObject() weak_obj = weakref.ref(obj)

print(weak_obj().my_method()) # Accessing the object via weak reference del obj print(weak_obj()) # Weak reference returns None as obj is no longer available`

These examples cover basic memory allocation, reference counting, explicit garbage collection calls, handling of cyclic references, and the use of weak references, all key components of Python's memory management system.

Explanation of Example 4: Cyclic References

In Example 4, we deal with the concept of cyclic references in Python and how they are managed:

pythonCopy code
a = []
b = [a]
a.append(b)  # Creates a cyclic reference
del a
del b
gc.collect()  # Garbage collector can handle cyclic references

What's Happening in This Example:

  • Creating Objects and Cyclic Reference:

  • a = []: This line creates an empty list and assigns it to the variable a.

  • b = [a]: Here, we create a new list b that contains the list a as its element.

  • a.append(b): This is the critical step. We append the list b to list a. Now, a contains b, and b contains a, creating a cyclic reference. Essentially, a references b, and b references a.

  • Deleting References:

  • del a: This deletes the reference a to the list it was pointing to. However, since b still holds a reference to this list, the list is not yet eligible for garbage collection.

  • del b: Similarly, this deletes the reference b. Now, both lists (a and b) are without any external references. But they still refer to each other, forming a cycle.

  • Garbage Collection:

  • gc.collect(): This explicitly invokes Python's garbage collector. Python's garbage collector is capable of detecting such cycles and can clean them up. It does so by breaking the cycle and deallocating the memory used by these objects, even though they are referring to each other.

Common Mistakes:

  • Misunderstanding Garbage Collection:

  • Assuming Python can't handle cyclic references: A common misconception is that Python's garbage collector cannot handle cyclic references, which can lead to memory leaks. In reality, Python's garbage collector is designed to detect and resolve such scenarios.

  • Forgetting to Delete References:

  • Not deleting references: If you forget to delete references (del a, del b), the objects remain in memory, potentially leading to memory inefficiency.

  • Overusing gc.collect():

  • Manually calling gc.collect() frequently: This can lead to performance overhead. The garbage collector is typically well-optimized for most use cases and does not require frequent manual intervention.

  • Creating Unnecessary Cyclic References:

  • Inadvertently creating cyclic references: Sometimes, cyclic references are created unintentionally due to complex data structures or functions. Being aware of object relationships can help prevent these issues.

  • Ignoring Weak References:

  • Not using weak references when appropriate: In some cases, using weak references (as shown in Example 5) can be a solution to avoid cyclic references, especially in cache implementations and observer patterns. Weak references allow one part of a reference cycle to be garbage collected.

Real-World Example: Managing Observers in a Publish-Subscribe Pattern

A real-world case where cyclic references might inadvertently occur, and where weak references are beneficial, is in the implementation of the Observer pattern, often used in event handling systems.


In a GUI application, you might have a model object that needs to update several observer widgets whenever its state changes. This is a classic publish-subscribe situation where the model is the publisher and the widgets are the subscribers.

Example Without Weak References (Potential Cyclic References):

pythonCopy code
class Model:
    def __init__(self):
        self.observers = []  # List of observer references

    def register_observer(self, observer):

    def notify_observers(self, message):
        for observer in self.observers:

class Widget:
    def __init__(self, model):
        self.model = model

    def update(self, message):
        print(f"Widget updated with message: {message}")

model = Model()
widget1 = Widget(model)
widget2 = Widget(model)

model.notify_observers("New Data Available")

In this setup, the Model holds strong references to its observers (widgets). If a widget is destroyed or no longer in use, the model still holds a reference to it, preventing it from being garbage collected. This situation can lead to memory leaks, especially in large applications with many dynamic widgets.

Refactoring with Weak References:

To prevent this, we can use weak references. Python's weakref module allows the model to reference its observers without preventing their garbage collection when they are no longer in use.

pythonCopy code
import weakref

class Model:
    def __init__(self):
        self.observers = []  # List of weak references to observer objects

    def register_observer(self, observer):

    def notify_observers(self, message):
        for observer_ref in self.observers:
            observer = observer_ref()
            if observer:  # Check if the observer still exists

# Widget class remains the same

How It Works and Why It's Used:

  • Weak References in Use:

  • When registering an observer, instead of adding a strong reference to the observer, a weak reference (weakref.ref(observer)) is stored.

  • During notification, the model checks if the observer still exists. If the observer has been garbage collected, observer_ref() returns None.

  • Advantages:

  • Automatic Memory Management: Weak references allow the Python garbage collector to remove observer objects when they are no longer needed, preventing memory leaks.

  • Decoupling of Components: The model doesn't need to know about the lifecycle of the observers. This decoupling is particularly useful in large applications where objects are dynamically created and destroyed.

  • Real-World Application:

  • This pattern is widely used in graphical user interface (GUI) frameworks, event-driven systems, and any scenario where a decoupled design is preferred. It provides a more efficient and robust way to handle dynamic relationships between objects.

Question 2: Can you explain the differences between Python 2 and Python 3?

There are several key changes in syntax, standard library, and underlying design philosophy. These changes were aimed at simplifying the code and making it more consistent and future-proof.


Example 1: Print Statement vs. Print Function

Python 2:

pythonCopy code
print "Hello, world!"  # Python 2 print statement

Python 3:

pythonCopy code
print("Hello, world!")  # Python 3 print function

Explanation: Python 2 uses the print statement without parentheses, whereas Python 3 uses the print function, which requires parentheses. This change enhances consistency with other functions in Python.

Real-World Use Case: Any basic script or application with output to the console needs to adapt to this change for compatibility with Python 3.

Example 2: Integer Division

Python 2:

pythonCopy code
result = 3/2  # Returns 1

Python 3:

pythonCopy code
result = 3/2  # Returns 1.5

Explanation: In Python 2, dividing two integers performs integer division. Python 3 changes this to true division, which returns a float.

Real-World Use Case: Financial calculations, scientific computations, or any application requiring precise decimal calculations must account for this change.

Example 3: Unicode String Handling

Python 2:

pythonCopy code
text = u"Hello, world!"  # Unicode string

Python 3:

pythonCopy code
text = "Hello, world!"  # Unicode by default

Explanation: Python 2 requires a 'u' prefix for Unicode strings. Python 3 treats all strings as Unicode by default, simplifying string handling in a global context.

Real-World Use Case: Applications dealing with internationalization, text processing, and web services benefit from the simplified Unicode handling in Python 3.

Example 4: xrange() vs range()

Python 2:

pythonCopy code
for i in xrange(5):  # Uses xrange for iteration
    print i

Python 3:

pythonCopy code
for i in range(5):  # range now behaves like Python 2's xrange

Explanation: Python 2 has two functions for iterations, range (returns a list) and xrange (returns an iterator). Python 3 eliminates xrange and modifies range to return an iterator.

Real-World Use Case: Loops in large-scale data processing applications need this change for memory efficiency in Python 3.

Example 5: Error Handling with 'as' Keyword

Python 2:

pythonCopy code
    # Some code
except Exception, e:
    print e

Python 3:

pythonCopy code
    # Some code
except Exception as e:

Explanation: Python 2 uses a comma to catch exceptions, while Python 3 uses the 'as' keyword for a more readable and clear syntax.

Real-World Use Case: Robust error handling in systems programming, web applications, and data pipelines requires this syntax adaptation in Python 3.

Question 3: How do you implement a Python decorator and what are its use cases?


Python decorators are a powerful and expressive feature for modifying the behavior of functions or methods. They allow for the extension or alteration of a function's behavior without modifying its actual code. Here are five examples of decorators along with explanations and real-world use cases:

Example 1: Basic Logging Decorator

pythonCopy code
def logger(func):
    def wrapper(*args, **kwargs):
        print(f"Executing {func.__name__} with arguments {args} and {kwargs}")
        return func(*args, **kwargs)
    return wrapper

def add(x, y):
    return x + y

add(5, 10)

Explanation: This decorator logs the function name and arguments every time the function is called. Real-World Use Case: Useful in debugging and monitoring, especially in web development and data processing pipelines.

Example 2: Execution Time Decorator

pythonCopy code
import time

def timing(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f"{func.__name__} took {end-start} seconds")
        return result
    return wrapper

def slow_function():


Explanation: Measures and prints the time taken by a function to execute. Real-World Use Case: Performance analysis in applications like data analysis, machine learning model training, or backend server optimization.

Example 3: Authentication Decorator

pythonCopy code
def authenticate(func):
    def wrapper(*args, **kwargs):
        if not user_is_authenticated:
            raise Exception("Authentication required")
        return func(*args, **kwargs)
    return wrapper

def sensitive_operation():

# Assume user_is_authenticated is defined somewhere

Explanation: Checks if a user is authenticated before allowing execution of the function. Real-World Use Case: Security in web applications, APIs, and any system requiring user authentication.

Example 4: Caching/Memoization Decorator

pythonCopy code
def cache(func):
    memo = {}
    def wrapper(*args):
        if args in memo:
            return memo[args]
        result = func(*args)
        memo[args] = result
        return result
    return wrapper

def compute_expensive_operation(x):
    # Simulate an expensive operation
    return x * x


Explanation: Caches the result of function calls to avoid repeated computation. Real-World Use Case: Optimizing performance in computational-intensive applications like scientific computing, data processing, and real-time systems.

Example 5: Parameter Validation Decorator

pythonCopy code
def validate_types(*arg_types):
    def decorator(func):
        def wrapper(*args):
            for (arg, arg_type) in zip(args, arg_types):
                if not isinstance(arg, arg_type):
                    raise TypeError(f"Argument {arg} is not of type {arg_type}")
            return func(*args)
        return wrapper
    return decorator

@validate_types(int, int)
def multiply(a, b):
    return a * b

multiply(2, 'not an int')  # Raises TypeError

Explanation: Validates the types of arguments passed to the function. Real-World Use Case: Type checking in critical applications like financial software, medical software, or any domain where data integrity is crucial.

Question 4: What is the Global Interpreter Lock (GIL) in Python and how does it affect multithreading?


The Global Interpreter Lock (GIL) is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes at once. This lock is necessary because Python's memory management is not thread-safe. Here are five examples demonstrating the GIL and its implications, along with real-world use cases:

Example 1: Simple Multithreading Without GIL Impact

pythonCopy code
import threading

def print_numbers():
    for i in range(1, 6):

thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_numbers)



Explanation: In this basic example, two threads execute a function that prints numbers. The GIL doesn't significantly affect performance as the task is I/O-bound. Real-World Use Case: I/O-bound multithreading operations in web scraping, file processing.

Example 2: CPU-Bound Operations Hindered by GIL

pythonCopy code
import threading, time

def compute_intensive():
    while True:
        pass  # Infinite loop representing a CPU-bound task

thread = threading.Thread(target=compute_intensive)

time.sleep(1)  # Main thread sleeps

Explanation: The infinite loop in a separate thread is a CPU-bound operation. The GIL will limit its performance because only one thread can execute Python bytecode at a time. Real-World Use Case: Situations where CPU-bound processing is required, highlighting the limitation of GIL in pure Python threading.

Example 3: Using Multiprocessing to Bypass GIL

pythonCopy code
import multiprocessing

def compute_task():
    # CPU-intensive task
    result = sum(i*i for i in range(10000000))

process1 = multiprocessing.Process(target=compute_task)
process2 = multiprocessing.Process(target=compute_task)



Explanation: Multiprocessing module creates separate Python interpreter processes which are not affected by GIL, thus providing true parallelism. Real-World Use Case: High-performance computing tasks, data processing where parallel CPU computation is required.

Example 4: Mixing I/O and CPU Tasks in Threads

pythonCopy code
import threading, time

def io_bound_task():
    time.sleep(2)  # Simulate I/O task

def cpu_bound_task():
    for _ in range(10000000):
        pass  # CPU-bound task

thread1 = threading.Thread(target=io_bound_task)
thread2 = threading.Thread(target=cpu_bound_task)



Explanation: Demonstrates how I/O-bound and CPU-bound tasks can coexist in a multithreaded environment. While one thread is waiting for I/O, the other can use CPU. Real-World Use Case: Web servers handling network I/O and data processing simultaneously.

Example 5: GIL Impact in Data Processing

pythonCopy code
import threading

def process_data():
    # Simulate data processing
    data = [i * 2 for i in range(1000000)]

threads = [threading.Thread(target=process_data) for _ in range(5)]

for thread in threads:

for thread in threads:

Explanation: Multiple threads performing a similar data processing task. The GIL will limit the concurrency of these operations. Real-World Use Case: Background tasks in web applications, batch data processing where GIL's limitations become apparent.

Real-World Use Cases of GIL

  • Web Development: In web applications, especially in I/O-bound scenarios like handling requests, reading files, or network communication, the GIL's impact is minimal.
  • Data Processing: When dealing with CPU-bound tasks like large-scale data analysis or scientific computations, the GIL can be a bottleneck, leading developers to use multiprocessing or external libraries that release the GIL.
  • Multithreading in GUI Applications: For GUI applications, Python's GIL allows simpler thread management for updating UI elements without worrying about data corruption due to concurrent access.

The GIL is a fundamental part of Python that has significant implications in multithreaded programming, influencing the choice of architecture and concurrency model in Python applications.

Question 5. Explain the concept of list comprehensions and provide an example.


Python list comprehensions offer a concise way to create lists. They consist of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The expressions can be anything, meaning you can put in all kinds of objects in lists. Here are five examples demonstrating list comprehensions, along with explanations and real-world use cases:

Example 1: Basic List Comprehension

pythonCopy code
squares = [x**2 for x in range(10)]

Explanation: This list comprehension creates a list of squares for numbers from 0 to 9. Real-World Use Case: Quick generation of mathematical sequences, data manipulation in data analysis tasks.

Example 2: List Comprehension with Conditional

pythonCopy code
even_squares = [x**2 for x in range(10) if x % 2 == 0]

Explanation: Generates a list of squares for even numbers only. Real-World Use Case: Filtering data in data processing, such as extracting specific elements from a dataset.

Example 3: Nested List Comprehension

pythonCopy code
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [num for row in matrix for num in row]

Explanation: Flattens a matrix (list of lists) into a single list. Real-World Use Case: Data flattening, useful in machine learning preprocessing to convert multi-dimensional data into a linear format.

Example 4: List Comprehension with Complex Conditions

pythonCopy code
filtered_data = [x for x in range(100) if x % 2 == 0 if x % 5 == 0]

Explanation: Filters numbers from 0 to 99 that are divisible by both 2 and 5. Real-World Use Case: Complex data filters in analytics, like filtering records based on multiple criteria.

Example 5: List Comprehension with Multiple Iterables

pythonCopy code
combined = [x + y for x in [1, 2, 3] for y in [4, 5, 6]]

Explanation: Combines two lists in a pairwise fashion, adding corresponding elements. Real-World Use Case: Data combination tasks, like merging datasets or creating Cartesian products for algorithmic processing.

Real-World Use Cases of List Comprehensions

  • Data Analysis and Science: List comprehensions are heavily used for data transformation, filtering, and aggregation in data analysis tasks, making the code more readable and concise.
  • Machine Learning: In feature engineering, list comprehensions are used to preprocess and transform datasets into the required format efficiently.
  • Web Development: They are used for extracting information from data structures, such as parsing JSON responses from APIs or processing query parameters.
  • Automation Scripts: Useful in scripting for file and data manipulation, such as reading files and processing lines with specific patterns.
  • Algorithm Development: Helpful in implementing algorithms, especially those involving mathematical computations and data transformations.

List comprehensions are a staple in Python programming, known for their readability and efficiency. They are particularly favored in areas where data manipulation and transformation are frequent.