← Back to Categories
Python Interview Questions
Comprehensive list of 30 questions to test your knowledge.
Theory & Concepts
1
What are the built-in data types in Python?
2
List vs Tuple?
3
Dictionary under the hood?
4
List comprehensions?
5
Memory management?
6
What is the GIL?
7
Generator vs function?
8
*args and **kwargs?
9
Context managers?
10
Threading vs Multiprocessing?
11
Yield keyword?
12
Magic methods?
13
Exception handling?
14
Lambda functions?
15
LEGB scope rule?
16
Deep vs shallow copy?
17
pip and virtualenv?
18
PEP 8?
19
Decorators?
20
Iterator vs Iterable?
21
if __name__ == '__main__'?
22
map/filter/reduce?
23
Duck typing?
24
Memory leaks?
25
@classmethod vs @staticmethod?
Hands-on & Coding Scenarios
1
Log File Parser
Context / Sample Data
File: server.log
2024-02-10 10:00:00 INFO User 123 logged in
2024-02-10 10:05:00 ERROR Database connection failed
2024-02-10 10:06:00 INFO User 456 logged in
2024-02-10 10:10:00 ERROR Timeout occurred
2024-02-10 10:15:00 WARN Disk usage at 85%
2024-02-10 10:20:00 ERROR Auth service unreachable
2024-02-10 10:25:00 INFO User 789 logged inTasks:
- Write a function to read this file and return the total count of ERROR messages.
- Extract all unique timestamps where an ERROR occurred as a sorted list.
- Write a regex pattern to extract all User IDs from the INFO lines.
Follow-up / Optimization:
- How to optimize for a 50GB log file?
- How to batch database inserts?
2
API Data Aggregation
Context / Sample Data
API Response (JSON):
[
{"user_id": 1, "purchase_amount": 100.50, "category": "electronics"},
{"user_id": 2, "purchase_amount": 50.00, "category": "books"},
{"user_id": 1, "purchase_amount": 200.00, "category": "electronics"},
{"user_id": 3, "purchase_amount": null, "category": "books"},
{"user_id": 2, "purchase_amount": 75.00, "category": "clothing"},
{"user_id": 1, "purchase_amount": 30.00, "category": "books"}
]Tasks:
- Calculate total purchase amount per user (skip nulls).
- Find the category with the highest total revenue.
- How to safely handle null purchase_amount?
Follow-up / Optimization:
- Refactor with collections.defaultdict?
- Handle API exceptions?
3
CSV Data Cleaning
Context / Sample Data
File: data.csv
id,name,email,signup_date
1,Alice,alice@gmail.com,2023-01-01
2,Bob,bob_at_yahoo.com,2023-01-05
3,Carol,,2023-02-10
4,Dave,dave@gmail.com,invalid_date
5,Eve,eve@company.org,2023-03-15Tasks:
- Print all rows with missing or empty email.
- Validate if an email address is properly formatted.
- Filter out invalid dates and write clean rows to clean_data.csv.
Follow-up / Optimization:
- Use pandas instead?
- Handle encoding errors?
4
Organizational Hierarchy Traversal
Context / Sample Data
Python Dictionary:
org_chart = {
'CEO': ['VP_Sales', 'VP_Eng'],
'VP_Sales': ['Dir_Sales'],
'VP_Eng': ['Dir_Eng1', 'Dir_Eng2'],
'Dir_Sales': ['Sales_A', 'Sales_B'],
'Dir_Eng1': ['Eng_A', 'Eng_B'],
'Dir_Eng2': ['Eng_C'],
'Sales_A': [],
'Sales_B': [],
'Eng_A': [],
'Eng_B': [],
'Eng_C': []
}Tasks:
- Count total people (direct+indirect) under a manager recursively.
- Find the direct manager of a specific employee.
- Print the hierarchy as an indented tree.
Follow-up / Optimization:
- Handle cycles?
- Rewrite iteratively?
5
Text Frequency Analysis
Context / Sample Data
Input Text:
text = """Data Engineering is amazing and challenging.
Data pipelines require robust engineering practices.
I love building data pipelines. Data is the new oil.
Engineering teams build amazing products."""Tasks:
- Count the frequency of each word, ignoring case and punctuation.
- Find the top 3 most frequent words.
- Remove common stop words via list comprehension.
Follow-up / Optimization:
- How does collections.Counter simplify this?
- Time complexity for 10M words?