Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fraudulent activity notifications #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions DSA/Python/Sorting/Problem Statement
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
HackerLand 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 a trailing number of days, they send the client a notification about potential fraud. The bank doesn't send the client any
notifications until they have at least that trailing number of prior days' transaction data. Given the number of trailing days d and a client's total daily expenditures for a
period of n days, determine the number of times the client will receive a notification over all n days.

Example
expenditure = [10, 20, 30, 40, 50]
d = 3
On the first three days,
they just collect spending data. At day 4, trailing expenditures are [10, 20, 30]. The median is 20 and the day's expenditure is 40. Because 40 > 2 x 20, there will be a notice.
The next day, trailing expenditures are [20, 30, 40] and the expenditures are 50. This is less than 2 x 30 so no notice will be sent. Over the period, there was one notice sent.

Note: The median of a list of numbers can be found by first sorting the numbers ascending. If there is an odd number of values, the middle one is picked.
If there is an even number of values, the median is then defined to be the average of the two middle values.

Function Description
Complete the function activityNotifications in the editor below.
activityNotifications has the following parameter(s):
• int expenditure[n]: daily expenditures
• int d: the lookback days for median spending

Returns
• int: the number of notices sent
Input Format
The first line contains two space-separated integers n and d, the number of days of transaction data, and the number of trailing days' data used to calculate median spending
respectively.
The second line contains n space-separated non-negative integers where each integer i denotes expenditure[i].

Constraints
• 1 < n < 2 x 105 • 1 <d< n • 0 < expenditure[i] < 200
16 changes: 16 additions & 0 deletions DSA/Python/Sorting/Solution
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from bisect import bisect_left, insort_left

n, d = map(int, input().split())
t = list(map(int, input().split()))
noti = 0

lastd = sorted(t[:d])
def med():
return lastd[d//2] if d % 2 == 1 else ((lastd[d//2] + lastd[d//2-1])/2)

for i in range(d, n):
if t[i] >= 2*med():
noti += 1
del lastd[bisect_left(lastd,t[i-d])]
insort_left(lastd, t[i])
print(noti)