Flag transactions that repeat an amount within a time window

Quick Overview

Given a time-ordered list of charges, return the indices of transactions whose amount already appeared within a fixed number of seconds before them. It tests hash map lookups over a sorted stream, inclusive window boundaries and edge cases such as equal timestamps and a timestamp of zero.

Flag transactions that repeat an amount within a time window

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of `transactions` in chronological order, where each transaction is `[timestamp, amount]`. Identify the potentially fraudulent ones. A transaction is potentially fraudulent if the same amount was charged at least once before within a window of `window_seconds` seconds (inclusive). More precisely, transaction `i` is fraudulent if there is an earlier transaction `j` (with `j < i`) that has the same amount and `transactions[i][0] - transactions[j][0] <= window_seconds`. Return the indices of all fraudulent transactions, sorted in ascending order. ### Function Signature ```python def detect_fraud(transactions: list[list[int]], window_seconds: int) -> list[int]: ``` ### Rules - Indices are 0-based. - "Earlier" means a smaller index. Two transactions with the same timestamp and the same amount make the later one fraudulent, because their difference is 0. - Any earlier transaction can be the match, including one that is itself fraudulent. - The window is inclusive: a difference of exactly `window_seconds` counts. - Return an empty list when no transaction is fraudulent. ### Constraints - `1 <= len(transactions) <= 10^5` - `0 <= timestamp <= 10^9` - `1 <= amount <= 10^6` - `1 <= window_seconds <= 10^9` - Transactions are sorted by `timestamp` in non-decreasing order. ### Examples ```text Input: transactions = [[0,100],[60,200],[120,100],[200,100],[250,200],[400,100]], window_seconds = 300 Output: [2,3,4,5] ``` Index 2 repeats the amount 100 from index 0 after 120 seconds, and index 3 repeats it after 200 seconds. Index 4 repeats 200 from index 1 after 190 seconds. Index 5 is 400 seconds after index 0, outside the window, but only 200 seconds after index 3, so it is flagged as well. ```text Input: transactions = [[0,50],[100,50],[500,50]], window_seconds = 60 Output: [] ``` Each repeat of 50 comes more than 60 seconds after every earlier charge of 50 (the closest gaps are 100 and 400 seconds), so nothing is flagged. ```text Input: transactions = [[10,5],[10,5],[15,7],[25,5]], window_seconds = 15 Output: [1,3] ``` Index 1 has the same timestamp and amount as index 0, a difference of 0. Index 3 is exactly 15 seconds after indices 0 and 1, and the window includes its edge. Index 2 is the only charge of 7.

Overview: Given a time-ordered list of charges, return the indices of transactions whose amount already appeared within a fixed number of seconds before them. It tests hash map lookups over a sorted stream, inclusive window boundaries and edge cases such as equal timestamps and a timestamp of zero.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of transactions in chronological order, where each transaction is [timestamp, amount]. Identify the potentially fraudulent ones.

A transaction is potentially fraudulent if the same amount was charged at least once before within a window of window_seconds seconds (inclusive). More precisely, transaction i is fraudulent if there is an earlier transaction j (with j < i) that has the same amount and transactions[i][0] - transactions[j][0] <= window_seconds.

Return the indices of all fraudulent transactions, sorted in ascending order.

Function Signature

def detect_fraud(transactions: list[list[int]], window_seconds: int) -> list[int]:

Rules

  • Indices are 0-based.
  • "Earlier" means a smaller index. Two transactions with the same timestamp and the same amount make the later one fraudulent, because their difference is 0.
  • Any earlier transaction can be the match, including one that is itself fraudulent.
  • The window is inclusive: a difference of exactly window_seconds counts.
  • Return an empty list when no transaction is fraudulent.

Constraints

  • 1 <= len(transactions) <= 10^5
  • 0 <= timestamp <= 10^9
  • 1 <= amount <= 10^6
  • 1 <= window_seconds <= 10^9
  • Transactions are sorted by timestamp in non-decreasing order.

Examples

Input: transactions = [[0,100],[60,200],[120,100],[200,100],[250,200],[400,100]], window_seconds = 300
Output: [2,3,4,5]

Index 2 repeats the amount 100 from index 0 after 120 seconds, and index 3 repeats it after 200 seconds. Index 4 repeats 200 from index 1 after 190 seconds. Index 5 is 400 seconds after index 0, outside the window, but only 200 seconds after index 3, so it is flagged as well.

Input: transactions = [[0,50],[100,50],[500,50]], window_seconds = 60
Output: []

Each repeat of 50 comes more than 60 seconds after every earlier charge of 50 (the closest gaps are 100 and 400 seconds), so nothing is flagged.

Input: transactions = [[10,5],[10,5],[15,7],[25,5]], window_seconds = 15
Output: [1,3]

Index 1 has the same timestamp and amount as index 0, a difference of 0. Index 3 is exactly 15 seconds after indices 0 and 1, and the window includes its edge. Index 2 is the only charge of 7.