Medium-Level Python Coding Questions for Data Science Interviews
1. Group and Rank:
You have a DataFrame with columns: customer_id, order_date, and purchase_amount.
Task: For each customer, rank their purchases by amount in descending order.
2. Rolling Averages:
Given a time series of daily sales, calculate a 7-day rolling average of sales.
3. Find the Longest Streak:
From a list of 'W' (win) and 'L' (loss), find the longest winning streak.
Example:
results = ['W', 'W', 'L', 'W', 'W', 'W', 'L']
Output: 3
4. Missing Ranges Detection:
Given a list of dates with gaps, return the missing date ranges.
Example:
dates = ['2024-01-01', '2024-01-02', '2024-01-04']
Output: ['2024-01-03']
5. Column Normalization:
Normalize numeric columns in a DataFrame using min-max scaling between 0 and 1.
6. Pivot Table Summary:
Given sales data, create a pivot table showing the total revenue by region and product.
7. Top N by Group:
From a DataFrame of students and scores, get the top 2 scorers per class.
8. Custom Aggregation:
Group a DataFrame by a category and return both the mean and standard deviation of a numeric column.
9. TF-IDF Sorting:
Given a list of text documents, compute TF-IDF and return the top 3 keywords per document.
10. Row-wise Operations:
Create a new column in a DataFrame that is:
- 'High' if the score > 80
- 'Medium' if 50 < score <= 80
- 'Low' if score <= 50
Use a lambda with apply.
11. Detect Outliers using IQR:
Write a function to detect and remove outliers using the Interquartile Range (IQR) method.
12. Datetime Grouping:
Given a datetime column, group data by week and return the total sum of sales per week.
13. Lag Features:
Given a time series, add a lag feature column (e.g., previous day's value).
14. Custom Function with apply:
Write a function to calculate BMI from weight and height columns, and apply it row-wise.
15. Explode a Column:
Given a column with comma-separated values, convert it to multiple rows.
Example:
df = [Link]({"id": [1, 2], "skills": ["Python,SQL", "Java"]})
Expected output:
id skills
1 Python
1 SQL
2 Java