PySpark Coding
Interview Questions
Hands-on PySpark problems: joins, window functions, aggregations, UDFs, and performance tuning scenarios.
Business Requirement
A bank wants to identify customers whose salary has significantly declined over the last 3 months.
Risk Criteria
- Customer should have monthly salary credits.
- Compare the latest month's salary with the average salary of the previous 3 months.
- If the salary has dropped by more than 40%, flag the customer.
- Ignore customers having fewer than 4 salary credits.
Input DataFrame
account_transactions
| account_id | txn_date | txn_type | amount |
|---|---|---|---|
| 1001 | 2024-01-05 | CREDIT | 100000 |
| 1001 | 2024-02-05 | CREDIT | 105000 |
| 1001 | 2024-03-05 | CREDIT | 98000 |
| 1001 | 2024-04-05 | CREDIT | 50000 |
| 1002 | 2024-01-05 | CREDIT | 70000 |
| 1002 | 2024-02-05 | CREDIT | 72000 |
| 1002 | 2024-03-05 | CREDIT | 71000 |
| 1002 | 2024-04-05 | CREDIT | 70000 |
Expected Output
| account_id | avg_previous_3_months | current_salary | drop_percentage | risk_flag |
|---|---|---|---|---|
| 1001 | 101000.00 | 50000 | 50.50% | FLAG |
| 1002 | 71000.00 | 70000 | 1.41% | NOT_FLAG |
Input DataFrame
Suppose we have this route data:
| source | destination | fare |
|---|---|---|
| Delhi | Mumbai | 5000 |
| Delhi | Pune | 3000 |
| Pune | Mumbai | 1500 |
| Delhi | Bangalore | 4000 |
| Bangalore | Mumbai | 2500 |
| Delhi | Chennai | 4500 |
| Chennai | Mumbai | 3000 |
We want to find the cheapest way to travel:
Delhi → Mumbai
Possible routes
| Route | Calculation | Total Fare |
|---|---|---|
| Delhi → Mumbai | 5000 | 5000 |
| Delhi → Pune → Mumbai | 3000 + 1500 | 4500 |
| Delhi → Bangalore → Mumbai | 4000 + 2500 | 6500 |
| Delhi → Chennai → Mumbai | 4500 + 3000 | 7500 |
Therefore:
Cheapest fare = ₹4,500
Route = Delhi → Pune → Mumbai
Logic Explain:
“I have a routes DataFrame containing source, destination and fare. To find a route through an intermediate city, I self-join the DataFrame. The destination of the first leg should match the source of the second leg. Then I add the two fares to calculate the total fare and order by total fare ascending. Finally, I take the first record, which gives me the cheapest route.
Remember the logic
Self Join → Find Intermediate City → Add Fares → Sort Ascending → limit(1)
1. Input DataFrame
Think of the DataFrame like this table:
| product_id | unit_price | quantity |
|---|---|---|
| 101 | 100.0 | 2 |
| 102 | 200.0 | 3 |
| 103 | 50.0 | 4 |
| 104 | 150.0 | 2 |
For each row:
Total Amount = unit_price × quantity
- Customer receives monthly salary credits.
- Compare latest month salary with average of previous 3 months.
- If salary drops by more than 40%, flag them.
- Ignore customers with fewer than 4 salary credits.
Output details
You are given two datasets:
1. Inventory Table
product_id
product_name
2. Transactions Table
transaction_id
time
product_id
Output the product name along with the transaction count and sort by product_id.
Handle duplicate transactions (keep latest)
Count the number of transactions per product
Exclude products with no transactions
Sort the result by product_id
Write a PySpark program to perform an JOIN and return only customers who have placed at least one order.
| student_id | attendance_date |
|---|---|
| 1 | 01-05-2025 |
| 2 | 01-05-2025 |
| 3 | 01-05-2025 |
| 1 | 02-05-2025 |
| 2 | 02-05-2025 |
| 3 | 02-05-2025 |
| 2 | 03-05-2025 |
| 3 | 03-05-2025 |
| 1 | 04-05-2025 |
| 2 | 04-05-2025 |
Table:Employee
| id | name | department |
|---|---|---|
| 1 | Amit | IT |
| 2 | Neha | HR |
| 3 | Amit | IT |
| 4 | Rahul | Sales |
| 5 | Neha | HR |