Detect Anomalies in Transaction Batches
PYTHON coding challenge · Difficulty: hard · Topic: Data Validation Pipeline · +200 XP
Problem
Given a list of transactions, each with an ID, amount, and timestamp, identify batches of consecutive transactions that sum up to a suspicious amount, say zero. This is crucial for data validation in financial pipelines to catch fraudulent activities where transactions cancel each other out.
Example Input
The data below is already defined — do not redefine it.
`python
transactions = [(1, 100, '2023-10-01T10:00:00'), (2, -50, '2023-10-01T10:05:00'), (3, -50, '2023-10-01T10:10:00'), (4, 200, '2023-10-01T10:15:00'), (5, -200, '2023-10-01T10:20:00')]
`
Expected Output
`
[(1, 3), (4, 5)]
`
Notes
- Print the result — the grader reads standard output
- Do not redefine the input; it is provided for you
What this PYTHON challenge teaches you
“Detect Anomalies in Transaction Batches” is a hard-level PYTHON challenge focused on Data Validation Pipeline. Working through it gives you hands-on practice with prefix sum, hash map, transactions — 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
- prefix sum
- hash map
- transactions
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 ≤ len(transactions) ≤ 100,000
- -10^9 ≤ transaction amount ≤ 10^9
- Each transaction is a tuple (id, amount, timestamp)
- Return a list of tuples, each representing the start and end indices of suspicious zero-sum batches
- Expected time complexity: O(N) — efficient detection of suspicious subarrays
How to practise it on PySpark.in
Open the challenge, write your Python 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 PYTHON challenges
- Validate Unique User IDs
- Word Frequency Counter
- Extract Error Codes from Log Strings
- Sum of Values by Key
- Flatten Nested JSON Structure
- Top K Frequent Elements in a Stream
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYTHON 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 hard and covers Data Validation Pipeline.