Flag suspicious procurement transactions by vendor average, same-day repeats and negative amounts

Quick Overview

Given procurement transactions with a vendor, an amount and a day, return the sorted ids that break any of three rules: an amount above three times the vendor average, repeat same-day activity for one vendor, or a negative amount. It tests hash map grouping and exact threshold comparisons.

Flag suspicious procurement transactions by vendor average, same-day repeats and negative amounts

Company: Sap

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a list of procurement transactions, return the ids of all suspicious transactions. Each transaction is a list `[transaction_id, vendor_id, amount, day]`. A transaction is suspicious if it meets any of these criteria: - Its `amount` is more than 3 times the average amount for its `vendor_id`. This rule applies only when that vendor's average is positive. - Its `vendor_id` has more than one transaction on the same `day`. In that case every transaction of that vendor on that day is suspicious. - Its `amount` is negative. Return the `transaction_id` values of all suspicious transactions, sorted in ascending order. ### Function Signature ```python def detect_anomalies(transactions: list[list]) -> list[int]: ``` ### Rules - Each transaction is `[transaction_id, vendor_id, amount, day]`, where `transaction_id` is an integer, `vendor_id` is a string, `amount` is an integer and `day` is an integer. - A vendor's average is the mean of all of that vendor's amounts, including negative amounts and the transaction being checked. - The first rule is strict: an amount equal to exactly 3 times the average is not suspicious. - The same-day rule counts transactions per vendor. Two different vendors with transactions on the same day do not trigger it. - A transaction that meets several criteria appears once in the output. Return an empty list when nothing is suspicious. ### Constraints - `1 <= len(transactions) <= 10^5` - `transaction_id` values are unique integers - `vendor_id` is a string - `-10^6 <= amount <= 10^6` - `1 <= day <= 365` ### Examples ```text Input: transactions = [[1, "V1", 100, 1], [2, "V1", 200, 1], [3, "V1", 150, 2], [4, "V2", 50, 3], [5, "V2", -30, 4], [6, "V2", 500, 5]] Output: [1, 2, 5] ``` Transactions 1 and 2 are both from V1 on day 1, and transaction 5 is negative. V2's average is 520 / 3, so the threshold is exactly 520, and transaction 6 (500) stays below it. ```text Input: transactions = [[10, "A", 100, 10], [11, "A", 100, 11], [12, "A", 100, 12], [13, "A", 1000, 13], [14, "B", -5, 1]] Output: [13, 14] ``` Vendor A averages 325, so its threshold is 975 and transaction 13 (1000) exceeds it. Transaction 14 is negative. ```text Input: transactions = [[7, "C", 300, 1], [8, "C", 0, 2], [9, "C", 0, 3], [20, "D", -10, 5], [21, "D", 5, 6]] Output: [20] ``` Vendor C averages 100, and transaction 7 equals the threshold of 300 without exceeding it. Vendor D's average is negative, so the first rule does not apply to transaction 21, and only the negative transaction 20 is flagged.

Overview: Given procurement transactions with a vendor, an amount and a day, return the sorted ids that break any of three rules: an amount above three times the vendor average, repeat same-day activity for one vendor, or a negative amount. It tests hash map grouping and exact threshold comparisons.

Sap logo
Sap
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a list of procurement transactions, return the ids of all suspicious transactions. Each transaction is a list [transaction_id, vendor_id, amount, day].

A transaction is suspicious if it meets any of these criteria:

  • Its amount is more than 3 times the average amount for its vendor_id . This rule applies only when that vendor's average is positive.
  • Its vendor_id has more than one transaction on the same day . In that case every transaction of that vendor on that day is suspicious.
  • Its amount is negative.

Return the transaction_id values of all suspicious transactions, sorted in ascending order.

Function Signature

def detect_anomalies(transactions: list[list]) -> list[int]:

Rules

  • Each transaction is [transaction_id, vendor_id, amount, day] , where transaction_id is an integer, vendor_id is a string, amount is an integer and day is an integer.
  • A vendor's average is the mean of all of that vendor's amounts, including negative amounts and the transaction being checked.
  • The first rule is strict: an amount equal to exactly 3 times the average is not suspicious.
  • The same-day rule counts transactions per vendor. Two different vendors with transactions on the same day do not trigger it.
  • A transaction that meets several criteria appears once in the output. Return an empty list when nothing is suspicious.

Constraints

  • 1 <= len(transactions) <= 10^5
  • transaction_id values are unique integers
  • vendor_id is a string
  • -10^6 <= amount <= 10^6
  • 1 <= day <= 365

Examples

Input: transactions = [[1, "V1", 100, 1], [2, "V1", 200, 1], [3, "V1", 150, 2], [4, "V2", 50, 3], [5, "V2", -30, 4], [6, "V2", 500, 5]]
Output: [1, 2, 5]

Transactions 1 and 2 are both from V1 on day 1, and transaction 5 is negative. V2's average is 520 / 3, so the threshold is exactly 520, and transaction 6 (500) stays below it.

Input: transactions = [[10, "A", 100, 10], [11, "A", 100, 11], [12, "A", 100, 12], [13, "A", 1000, 13], [14, "B", -5, 1]]
Output: [13, 14]

Vendor A averages 325, so its threshold is 975 and transaction 13 (1000) exceeds it. Transaction 14 is negative.

Input: transactions = [[7, "C", 300, 1], [8, "C", 0, 2], [9, "C", 0, 3], [20, "D", -10, 5], [21, "D", 5, 6]]
Output: [20]

Vendor C averages 100, and transaction 7 equals the threshold of 300 without exceeding it. Vendor D's average is negative, so the first rule does not apply to transaction 21, and only the negative transaction 20 is flagged.