DSA sliding window learning path Level 3 This level · 1 of 3
Medium dsa sliding-window

Fraudulent Activity Notifications

The 30 second answer

For each day after a trailing window of d days, the bank sends a notice when that day's spend is at least twice the median of the window. Official constraints cap each spend at 200, so I keep a counting array of frequencies, look up the median in linear time over that small range, then slide the window by incrementing today and decrementing the day that just aged out.

Could you say this in an interview?

Problem A bank looks at a trailing window of d daily expenditures and, from day d onwards, sends a notification whenever that day's spend is greater than or equal to twice the window's median. The median of an odd-length window is the middle value after sorting; for an even length it is the average of the two middle values. Return how many notifications are sent. There is no notification until d prior days exist. Original: HackerRank: Fraudulent Activity Notifications.

Understanding the prompt The first d days only fill the window. The comparison is expenditure[i] >= 2 * median, so for an even d you can compare against the sum of the two central values and stay in integers. Official constraints put every expenditure in 0 to 200, which is what makes a counting array the right median structure.

Pattern recognition A fixed-size sliding window whose interesting query is the median. With a tiny value range, a frequency array plus a walk to the k-th element replaces a sort or a two-heap structure at each step.

Brute force For every i >= d, copy and sort the previous d values: O(n · d log d), which is too slow for the official limits.

Optimal Fill a frequency array of size 201 from the first d days. For each later day, compute twice the median by walking the frequencies to the needed rank (the middle element if d is odd, the two central ranks if d is even). If today's spend is at least that value, count a notice. Then increment today's frequency and decrement the frequency of the day that left the window.

Alternatives Two heaps, or a balanced tree of the window, also give a running median and would be the answer if the values were not bounded. They are heavier to implement and slower than a 201-slot count.

Java solution

public class FraudulentActivityNotifications {
    static final int MAX_EXPENDITURE = 200;

    static int activityNotifications(int[] expenditure, int d) {
        var freq = new int[MAX_EXPENDITURE + 1];
        for (int i = 0; i < d; i++) freq[expenditure[i]]++;
        int notices = 0;
        for (int i = d; i < expenditure.length; i++) {
            if (expenditure[i] >= twiceMedian(freq, d)) notices++;
            freq[expenditure[i]]++;
            freq[expenditure[i - d]]--;
        }
        return notices;
    }

    // Returns 2 * median of the window represented by freq, so the caller can compare with integers.
    static int twiceMedian(int[] freq, int d) {
        if (d % 2 == 1) return 2 * kth(freq, d / 2 + 1);
        return kth(freq, d / 2) + kth(freq, d / 2 + 1);
    }

    static int kth(int[] freq, int k) { // 1-based rank in the multiset
        int seen = 0;
        for (int v = 0; v < freq.length; v++) {
            seen += freq[v];
            if (seen >= k) return v;
        }
        throw new IllegalStateException("rank out of range");
    }

    public static void main(String[] args) {
        System.out.println(activityNotifications(new int[] {2, 3, 4, 2, 3, 6, 8, 4, 5}, 5)); // 2
        System.out.println(activityNotifications(new int[] {1, 2, 3, 4, 4}, 4));             // 0
    }
}

Complexity O(n · R) time, where R is the official value range of 201, and O(R) extra space. That is linear in n for this problem's constraints.

Edge cases

  • Even d, as in [1,2,3,4,4] with d = 4: the median of [1,2,3,4] is 2.5, 2 * median is 5, and a spend of 4 does not notify, so the answer is 0.
  • Odd d with several notices, as in the official sample of nine days and d = 5, which sends 2 notices.
  • d equal to n: the window never has a following day, so the answer is 0.

Follow-ups

  • Find Median from Data Stream is the unbounded-value version, usually solved with two heaps.
  • If the interviewer does not state a small value range, do not reach for a counting array; say you would use two heaps.

Tests The official HackerRank samples: expenditure [2,3,4,2,3,6,8,4,5] with d=5 returns 2; [1,2,3,4,4] with d=4 returns 0.

How to say it in the interview "I need a running median of the last d days, and the official values only go up to 200, so I keep a counting array. I look up twice the median by walking those 201 slots to the middle rank, compare it with today, then slide the window with a plus one and a minus one. That beats resorting the window every day."

Sources (1)
  1. HackerRank: Fraudulent Activity Notificationshackerrank.com
Previous
Next