Skip to content

Instantly share code, notes, and snippets.

@horlabyc
Last active March 20, 2023 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horlabyc/2e537828beddbabb442ed4d7c93ae06a to your computer and use it in GitHub Desktop.
Save horlabyc/2e537828beddbabb442ed4d7c93ae06a to your computer and use it in GitHub Desktop.
Fraud detection script
// d = Number of prior transaction days
// transactions
function calculateNumberOfNotification(d, transactions) {
const n = transactions.length;
let numberOfNotifications = 0;
if (!n) {
return numberOfNotifications
}
if (n <= d) {
return numberOfNotifications
}
// Get the median index of the prior expenditures days
const midean = Math.floor(d / 2);
// Slice the transactions to get the first 0 - d transactions and sort it by ascending order
const sortedExpenditures = transactions.slice(0,d).sort((a, b) => a - b);
// Get the median expenditure
let medianExpenditure = sortedExpenditures.length % 2 !== 0 ? sortedExpenditures[midean] : (sortedExpenditures[midean - 1] + sortedExpenditures[midean]) / 2;
// Loop through all the transactions starting from the transaction after the prior days
// After each loop, remove the transaction at the beginning of the list, add a new transaction to the list, and recalculate the new median
// Then determine if there is need to send a notification for the new transaction
for(let i = d; i < n; i++){
if (transactions[i] >= 2 * medianExpenditure) {
numberOfNotifications++;
}
let indexOfTransactionToRemove = sortedExpenditures.indexOf(transactions[i-d]);
sortedExpenditures.splice(indexOfTransactionToRemove, 1);
let indexOfTransactionToAdd = sortedExpenditures.findIndex(x => x > transactions[i]);
if (indexOfTransactionToAdd === -1) {
sortedExpenditures.push(transactions[i]);
} else {
sortedExpenditures.splice(indexOfTransactionToAdd, 0, transactions[i]);
}
if (d % 2 === 0) {
const midean = Math.floor(d / 2);
mediaExpenditure = (sortedExpenditures[midean - 1] + sortedExpenditures[midean]) / 2
} else {
medianExpenditure = sortedExpenditures[Math.floor(d/2)];
}
}
return numberOfNotifications;
}
const number = calculateNumberOfNotification(5, [2, 3, 4, 2, 3, 6, 8, 4, 5])
console.log({ number })
@horlabyc
Copy link
Author

USE CASE 3

Seamfix National Bank has a simple policy for warning clients about possible fraudulent account activity.
If the amount spent by a client on a particular day is greater than or equal to 2x the client's median spending for the last d days, they send the client a notification about potential fraud.
The bank doesn't send the client any notifications until they have at least d prior days of transaction data.
Given the value of d and a client's total daily expenditures for a period of n days, find and print the number of times the client will receive a notification over all n days.

Note: The median of a list of numbers can be found by arranging all the numbers from smallest to greatest.
If there is an odd number of numbers, the middle one is picked. If there is an even number of numbers, the median is then defined to be the average of the two middle values. (Wikipedia)
Input Format
The first line contains two space-separated integers denoting the respective values of n (the number of days there is transaction data for) and d (the number of prior days the bank uses to calculate median spending).
The second line contains n space-separated non-negative integers where each integer i denotes expenditure (i.e., the client's total expenditure for day i).
Constraints
. 1 <= n <= (2 * 10^5)
. 1 <= d <= n
. 0 <= expenditure <= 200

Output Format
Print an integer denoting the total number of times the client receives a notification over a period of n days.

Sample Input
9 5
2 3 4 2 3 6 8 4 5
Expected Output
2

Explanation
We must determine the total number of notifications the client receives over a period of n = 9 days.
For the first five days, the customer receives no notifications because the bank has insufficient transaction data and notifications = 0.

On the sixth day, the bank has d = 5 days of prior transaction data, {2,3,4,2,3}, and median = 3 dollars.
The client spends 6 dollars, which triggers a notification because 6 >= (2 * median). Thus, notifications = 1.

On the seventh day, the bank has d = 5 days of prior transaction data, {3,4,2,3,6}, and median = 3 dollars.
The client spends 8 dollars, which triggers a notification because 8 >= (2 * median). Thus, notifications = 1 + 1 = 2.

On the eighth day, the bank has d = 5 days of prior transaction data, {4,2,3,6,8}, and median = 4 dollars.
The client spends 4 dollars, which does not trigger a notification because 4 < (2 * median).

On the ninth day, the bank has d = 5 days of prior transaction data, {2,3,6,8,4}, and a transaction median of 4 dollars.
The client spends 5 dollars, which does not trigger a notification because 5 < (2 * median).
We then print the final value of notifications (which is 2) as our answer.
Express the above as a software algorithm using Nodejs. Only original solutions would be accepted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment