Detect Significant Salary Declines

PYSPARK coding challenge · Difficulty: medium · Topic: Window Functions · +150 XP

Problem

A bank monitors payroll credits to spot customers whose income has fallen

sharply, because a sudden drop is an early signal of repayment risk.

account_transactions holds every transaction on an account. A **salary

credit** is any row where txn_type = 'CREDIT'.

For each account, compare the latest salary credit against the average of

the three salary credits immediately before it. Report the account when

that fall is more than 40%.

Table

Table: account_transactions

| Column | Type | Description |
| --- | --- | --- |
| `account_id` | INT | account identifier |
| `txn_date` | DATE | date of the transaction |
| `txn_type` | VARCHAR | `CREDIT` or `DEBIT` |
| `amount` | DECIMAL(12,2) | transaction amount |

Output

| Column | Meaning |
| --- | --- |
| `account_id` | the account |
| `avg_previous_3_salary` | mean of the three credits before the latest, to 2 decimals |
| `current_salary` | the latest salary credit, to 2 decimals |
| `salary_drop_percentage` | the fall, to 2 decimals |
| `risk_flag` | the literal `FLAGGED` |

Formula

`

average_previous_3 = (salary_2 + salary_3 + salary_4) / 3

salary_drop_percentage = ((average_previous_3 - latest_salary)

/ average_previous_3) * 100

`

salary_1 is the newest credit, salary_2 the one before it, and so on.

Rules

1. Only txn_type = 'CREDIT' rows are salary. **DEBIT rows are ignored

entirely**, including when deciding which credit is the latest.

2. Order credits by txn_date. salary_1 is the most recent.

3. Use exactly the three credits before the latest. If an account has more

than four credits, the older ones are not part of the average.

4. An account needs at least four salary credits. Fewer than four is

excluded, however steep the fall.

5. Flag only when salary_drop_percentage > 40. Exactly 40 is not

flagged.

6. Round avg_previous_3_salary, current_salary and

salary_drop_percentage to 2 decimals.

7. Do not hardcode account ids.

8. Sort by account_id ascending. Return no rows if nobody qualifies.

Example

account_transactions:

| account_id | txn_date | txn_type | amount |
| --- | --- | --- | --- |
| 1001 | 2024-01-05 | CREDIT | 100000.00 |
| 1001 | 2024-02-05 | CREDIT | 105000.00 |
| 1001 | 2024-03-05 | CREDIT | 98000.00 |
| 1001 | 2024-04-05 | CREDIT | 50000.00 |
| 1002 | 2024-01-05 | CREDIT | 70000.00 |
| 1002 | 2024-02-05 | CREDIT | 72000.00 |
| 1002 | 2024-03-05 | CREDIT | 71000.00 |
| 1002 | 2024-04-05 | CREDIT | 70000.00 |

Expected output:

| account_id | avg_previous_3_salary | current_salary | salary_drop_percentage | risk_flag |
| --- | --- | --- | --- | --- |
| 1001 | 101000.00 | 50000.00 | 50.50 | FLAGGED |

Explanation

Account 1001: the latest credit is 50000. The three before it are

100000, 105000 and 98000, averaging 101000.

`

((101000 - 50000) / 101000) * 100 = 50.4950... -> 50.50

`

50.50 > 40, so 1001 is flagged.

Account 1002 falls from an average of 71000 to 70000, about 1.41%, so

it does not appear.

Constraints

Concepts

ROW_NUMBER(), PARTITION BY, CTEs, conditional aggregation, and a HAVING

style count guard. In PySpark: Window, row_number(), groupBy, agg,

filter and round.

What this PYSPARK challenge teaches you

“Detect Significant Salary Declines” is a medium-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with Window, row_number, groupBy, agg, when, filter — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Filter to CREDIT first, then rank with row_number() over Window.partitionBy('account_id').orderBy(F.col('txn_date').desc()). DEBIT rows must not affect the ranking.
  2. Split the ranked frame in two: rn == 1 gives the current salary, rn.between(2, 4) gives the history. On the history use groupBy('account_id').agg(avg, count) and keep only groups where the count is exactly 3.
  3. Join the two frames on account_id, add the drop percentage, filter > 40, then round and cast the numeric columns and finish with orderBy('account_id').

Where this comes up

Variations of this problem have been reported in interviews at RBS. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related PYSPARK challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Window Functions.

Solve this challenge free on PySpark.in