Python Interview Questions
Python Interview Questions
Basic
1. What are the key features of Python?
o Python is easy to learn, interpreted, dynamically typed, and supports
multiple programming paradigms (procedural, object-oriented,
functional).
o It has a large standard library and is cross-platform.
2. What is the difference between a list and a tuple in Python?
o Lists are mutable, meaning their contents can be changed after creation.
Tuples are immutable, so their contents cannot be modified once created.
3. What is the purpose of self in Python?
o self refers to the instance of the class. It is used to access variables that
belong to the class.
4. Explain the difference between is and == in Python.
o == checks if the values of two objects are equal.
o is checks if two objects refer to the same memory location.
5. What is a lambda function in Python?
o A lambda function is an anonymous, small function defined using the
lambda keyword, which can have any number of arguments but only one
expression.
o Example: multiply = lambda x, y: x * y
6. What are Python decorators?
o Decorators are functions that modify the behavior of other functions or
methods. They are commonly used to add functionality like logging,
caching, or access control.
o Example:
Python code
def decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@decorator
def say_hello():
print("Hello")
7. What is the difference between del and remove() in Python?
o del is used to delete variables or elements from a list by index or delete
entire objects.
o remove() is used to remove the first occurrence of a specific value in a list.
8. What are Python’s built-in data types?
o Integers (int), floating-point numbers (float), complex numbers (complex),
strings (str), lists (list), tuples (tuple), sets (set), dictionaries (dict).
9. What are list comprehensions in Python?
o List comprehensions provide a concise way to create lists by applying an
expression to each element of an iterable.
o Example: [x for x in range(5)] # [0, 1, 2, 3, 4]
10. What are *args and **kwargs in Python?
o *args is used to pass a variable number of non-keyword arguments to a
function.
o **kwargs is used to pass a variable number of keyword arguments to a
function.
# Dictionary comprehension
square_dict = {x: x**2 for x in range(5)}
6. What is the purpose of yield in Python?
o yield is used in functions to create a generator. It allows the function to
return an intermediate result and resume from the point it left off, which
helps save memory when dealing with large datasets.
7. What are Python’s metaclasses?
o Metaclasses define the behavior of classes themselves. They can be used
to control the creation, instantiation, and inheritance of classes.
8. What is the difference between __str__ and __repr__ in Python?
o __str__: Defines the “informal” string representation of an object, which is
user-friendly.
o __repr__: Defines the “formal” string representation of an object, which is
more for debugging and development.
9. How does Python handle memory management?
o Python uses reference counting and garbage collection to manage
memory. When an object's reference count reaches zero, it is deleted. The
garbage collector cleans up cyclic references.
10. What is the use of super() in Python?
o super() is used to call methods from a parent class. It is often used in
method overriding in class inheritance to call the parent class's method.
print(fibonacci(6)) # Output: 8
print(sort_by_second_element([(1, 2), (3, 1), (5, 0)])) # Output: [(5, 0), (3, 1), (1, 2)]
4. Check Anagram
o Problem: Write a function that checks if two strings are anagrams.
python code
def are_anagrams(s1, s2):
return sorted(s1) == sorted(s2)
print(are_anagrams("listen", "silent")) # Output: True
5. Find All Prime Numbers in a Range
o Problem: Write a function to find all prime numbers between two
numbers.
python code
def find_primes_in_range(start, end):
primes = []
for num in range(start, end + 1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
primes.append(num)
return primes
print(find_primes_in_range(10, 50)) # Output: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
6. Merge Two Sorted Lists
o Problem: Write a function that merges two sorted lists into one sorted list.
python code
def merge_sorted_lists(list1, list2):
return sorted(list1 + list2)
print(merge_sorted_lists([1, 3, 5], [2, 4, 6])) # Output: [1, 2, 3, 4, 5, 6]
result = ""
for i in range(len(s)):
odd_palindrome = expand_around_center(i, i)
even_palindrome = expand_around_center(i, i + 1)
result = max(result, odd_palindrome, even_palindrome, key=len)
return result
print(subsets([1, 2, 3])) # Output: [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
7. Trapping Rain Water
o Problem: Given an array of heights, calculate how much water can be
trapped between the bars.
python code
def trap(height):
left, right = 0, len(height) - 1
left_max, right_max = 0, 0
water_trapped = 0
while left <= right:
if height[left] <= height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
water_trapped += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
water_trapped += right_max - height[right]
right -= 1
return water_trapped
print(trap([0,1,0,2,1,0,1,3,2,1,2,1])) # Output: 6
8. Coin Change Problem (Dynamic Programming)
o Problem: Given a set of coins, find the minimum number of coins needed
to make a given amount.
python code
def coin_change(coins, amount):
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for coin in coins:
for i in range(coin, amount + 1):
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount] if dp[amount] != float('inf') else -1
DSA
Basic DSA Interview Questions:
1. What is an array? How is it different from a linked list?
o An array is a collection of elements stored at contiguous memory
locations. The size of an array is fixed, and accessing elements by index is
fast (O(1)).
o A linked list is a collection of nodes where each node contains data and a
reference (or link) to the next node. It allows dynamic memory allocation
but has slower access time (O(n)).
2. What are the types of linked lists?
o Singly Linked List: Each node points to the next node.
o Doubly Linked List: Each node has two references, one to the next node
and another to the previous node.
o Circular Linked List: The last node points back to the first node, making
the list circular.
3. What is a stack?
o A stack is a linear data structure that follows the Last In, First Out (LIFO)
principle. Operations include push() (insert), pop() (remove), and peek()
(view the top element).
4. What is a queue?
o A queue is a linear data structure that follows the First In, First Out (FIFO)
principle. Operations include enqueue() (insert) and dequeue() (remove).
5. What are the differences between a stack and a queue?
o Stack follows LIFO order, whereas a queue follows FIFO order. Stack
operations are push() and pop(), while queue operations are enqueue()
and dequeue().
6. What is a hash table?
o A hash table (or hash map) is a data structure that maps keys to values for
efficient lookup. It uses a hash function to compute an index into an array
of buckets or slots, from which the desired value can be found.
7. What is a binary tree?
o A binary tree is a tree data structure where each node has at most two
children, referred to as the left and right children.
8. What is the difference between a binary tree and a binary search tree (BST)?
o A binary tree is a tree structure with at most two children per node, while a
binary search tree (BST) is a binary tree with the condition that for every
node, the left child is smaller and the right child is greater than the node.
9. What is a heap?
o A heap is a specialized tree-based data structure that satisfies the heap
property. In a max-heap, the value of the parent node is greater than or
equal to the values of the children, and in a min-heap, the value of the
parent node is less than or equal to the values of the children.
10. What is the difference between a tree and a graph?
o A tree is a hierarchical structure with a single root node and no cycles,
while a graph is a collection of nodes connected by edges, and it may
contain cycles.
Django
Django Interview Questions:
1. What is Django and what are its key features?
o Django is a high-level Python web framework that enables rapid
development of secure and maintainable websites.
o Key features include:
▪ Built-in ORM (Object-Relational Mapping)
▪ URL routing
▪ Middleware support
▪ Form handling
▪ Template engine
▪ Security features like CSRF protection, authentication, and session
management
2. What is the difference between Django’s QuerySet and Manager?
o A QuerySet is a collection of database queries that Django’s ORM
generates. It represents a set of records from the database.
o A Manager is an interface for querying the database. It's an instance of
django.db.models.Manager, typically used for adding custom query
methods.
3. Explain Django’s MVC architecture.
o Django follows the MVC (Model-View-Controller) design pattern, but it’s
often referred to as MVT (Model-View-Template):
▪ Model: Represents the data structure (i.e., database schema).
▪ View: Handles the logic and business rules, processing user
requests.
▪ Template: Represents the user interface, how the data is
displayed.
4. What are Django middleware?
o Middleware are hooks into Django’s request/response processing. They
are classes that process the request before it reaches the view and after
the view has processed it.
o They can be used for tasks like authentication, logging, and session
management.
5. What is the difference between ForeignKey, OneToOneField, and
ManyToManyField in Django models?
o ForeignKey: Represents a many-to-one relationship.
o OneToOneField: Represents a one-to-one relationship.
o ManyToManyField: Represents a many-to-many relationship.
6. How do you handle form submissions in Django?
o Django provides a Form class that helps you handle form validation,
rendering, and processing. You can use it with the POST method to collect
user input.
o Example:
Python code
from django import forms
class ContactForm(forms.Form):
name = forms.CharField()
email = forms.EmailField()
def contact_view(request):
if request.method == "POST":
form = ContactForm(request.POST)
if form.is_valid():
# Process form data
pass
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
7. Explain the purpose of Django migrations.
o Migrations are a way of propagating changes to your database schema
over time. They help you keep track of changes in your models and apply
those changes to the database.
8. What are Django signals?
o Django signals allow certain senders to notify a set of receivers when
certain actions occur. For example, when a model is saved, you can use
the post_save signal to trigger actions like sending an email.
9. What is the Django ORM?
o The Django ORM (Object-Relational Mapping) is a way to interact with the
database using Python code rather than raw SQL. It maps Python classes
to database tables, allowing you to work with the database in an object-
oriented way.
10. How can you improve the performance of Django applications?
o Use database indexing to optimize queries.
o Cache results of expensive database queries or views.
o Compress static files and use a content delivery network (CDN).
o Optimize database queries by using select_related and prefetch_related
to reduce the number of database hits.
o Use lazy loading for large datasets.
ML
Basic Machine Learning Questions:
1. What is Machine Learning?
o Answer: Machine learning is a subset of artificial intelligence (AI) that
allows systems to automatically learn and improve from experience
without being explicitly programmed. It involves training models using data
to make predictions or decisions based on input.
2. What are the types of Machine Learning?
o Answer: The three main types of machine learning are:
▪ Supervised Learning: The model is trained using labeled data,
where the output is known.
▪ Unsupervised Learning: The model is trained using unlabeled
data, with no explicit output labels.
▪ Reinforcement Learning: The model learns by interacting with an
environment and receiving rewards or penalties.
3. What is the difference between classification and regression?
o Answer:
▪ Classification: The task of predicting discrete labels or classes
(e.g., spam or not spam).
▪ Regression: The task of predicting continuous values (e.g.,
predicting house prices).
4. What is overfitting and underfitting?
o Answer:
▪ Overfitting: When a model learns too much from the training data,
including noise, and performs poorly on new data.
▪ Underfitting: When a model is too simple to capture the underlying
patterns in the data, leading to poor performance on both training
and test data.
5. What is cross-validation?
o Answer: Cross-validation is a technique used to assess the performance
of a model by splitting the data into multiple subsets and training and
validating the model on different subsets. It helps in preventing overfitting
and ensuring the model generalizes well.
6. What is a confusion matrix?
o Answer: A confusion matrix is a table used to evaluate the performance of
a classification algorithm. It shows the counts of true positive, false
positive, true negative, and false negative predictions.
7. What is precision, recall, and F1-score?
o Answer:
▪ Precision: The proportion of positive predictions that are actually
correct.
▪ Recall: The proportion of actual positives that are correctly
identified by the model.
▪ F1-Score: The harmonic mean of precision and recall, providing a
balance between the two.
8. What is the difference between bagging and boosting?
o Answer:
▪ Bagging (Bootstrap Aggregating): Uses multiple models (e.g.,
decision trees) trained on random subsets of the data, and
averages their predictions. Example: Random Forest.
▪ Boosting: Sequentially trains models, where each model tries to
correct the errors of the previous one. Example: Gradient Boosting,
AdaBoost.
9. What is the bias-variance tradeoff?
o Answer: The bias-variance tradeoff refers to the balance between a
model’s ability to generalize and its ability to fit the training data:
▪ Bias: Error due to overly simplistic models (underfitting).
▪ Variance: Error due to overly complex models (overfitting).
10. What is the purpose of feature scaling?
o Answer: Feature scaling (normalization or standardization) is the process
of adjusting the values of features to a similar scale, ensuring that no single
feature dominates others in the training process. It is especially important
for algorithms like KNN and SVM.
DL
AI
DATA SCIENCE
Basic Data Science Questions:
1. What is Data Science?
o Answer: Data Science is a multidisciplinary field that uses scientific
methods, processes, algorithms, and systems to extract knowledge and
insights from structured and unstructured data. It combines statistics,
data analysis, machine learning, and computer science to solve complex
problems.
2. What is the difference between Data Science and Data Analytics?
o Answer:
▪ Data Science: Involves extracting actionable insights from data
using advanced techniques like machine learning, predictive
modeling, and big data technologies.
▪ Data Analytics: Focuses on examining data to draw conclusions
about the information and is more descriptive and exploratory,
often using basic statistical techniques.
3. What is the difference between structured and unstructured data?
o Answer:
▪ Structured Data: Data that is organized in rows and columns (e.g.,
relational databases, spreadsheets).
▪ Unstructured Data: Data that does not have a predefined format or
structure (e.g., text, images, videos, social media posts).
4. What is the role of a Data Scientist?
o Answer: A Data Scientist is responsible for collecting, cleaning, and
analyzing data, using statistical techniques, machine learning models,
and data visualization tools to extract insights and make data-driven
decisions.
5. What is Data Cleaning?
o Answer: Data cleaning is the process of identifying and correcting errors
or inconsistencies in data to improve its quality and make it suitable for
analysis. This includes handling missing values, duplicate records, and
outliers.
6. What are the steps involved in a Data Science project?
o Answer: The typical steps are:
1. Problem Definition
2. Data Collection
3. Data Cleaning and Preprocessing
4. Exploratory Data Analysis (EDA)
5. Feature Engineering
6. Model Building
7. Model Evaluation
8. Model Deployment
9. Monitoring and Maintenance
7. What is Exploratory Data Analysis (EDA)?
o Answer: EDA is the process of analyzing and visualizing datasets to
summarize their main characteristics, often with the help of statistical
graphics, plots, and information tables. The goal is to identify patterns,
anomalies, relationships, and insights.
8. What is the importance of data visualization in Data Science?
o Answer: Data visualization helps in communicating findings effectively,
making complex data more understandable, and revealing insights that
might not be apparent through raw data alone. Common tools include
Matplotlib, Seaborn, and Tableau.
9. What are outliers, and how do you handle them?
o Answer: Outliers are data points that are significantly different from the
rest of the data. They can be handled by:
▪ Removing them if they are errors.
▪ Using techniques like Z-scores or IQR to identify them.
▪ Transforming or capping outliers to reduce their impact.
10. What is feature engineering?
o Answer: Feature engineering is the process of selecting, modifying, or
creating new features (variables) from raw data to improve the
performance of machine learning models. It includes tasks like scaling,
encoding categorical variables, and creating interaction features.