Arrays and Strings: The Sliding Window challenge
Imagine a bank in Nigeria that has a mobile banking app, where customers perform several transactions throughout the day. The bank needs to identify certain patterns in a customer's transaction behavior. For instance, the bank may want to identify if the total transaction amount for any 7-day sliding window exceeds a certain threshold, which could indicate unusual or potentially fraudulent behavior.
- Application in Banking: The sliding window technique can be used to analyze the sum of transactions within a certain time period (7 days, in this case) and alert the bank if a user’s daily average is significantly high. This is useful for fraud detection or monitoring customer spending patterns over time.
Real-World Problem Using Sliding Window:
Let's say you are given an array of transactions made by a customer over n
days, and you need to find out the maximum sum of transactions in any 7-day period. This would be a typical case where the sliding window technique is useful.
Problem:
Given an array of transaction amounts for n
days, find the maximum sum of transactions over any 7-day sliding window.
For example:
Input:
[500, 200, 300, 100, 50, 400, 350, 200, 600, 800]
Window size: 7 days
Expected Output:
The maximum sum of transactions over any 7-day period.
Steps to Solve:
Initialize a window that includes the sum of the first 7 elements (days 1-7).
Slide the window one element at a time by:
Subtracting the transaction amount of the day that’s no longer in the window.
Adding the transaction amount of the new day entering the window.
Keep track of the maximum sum encountered as the window slides across the entire array.
Code Example (Python):
def max_transaction_sum(transactions, window_size):
# Edge case: If the number of days is less than the window size, return the sum of all transactions
if len(transactions) < window_size:
return sum(transactions)
# Calculate the sum of the first window
window_sum = sum(transactions[:window_size])
max_sum = window_sum
# Slide the window across the transactions array
for i in range(len(transactions) - window_size):
window_sum = window_sum - transactions[i] + transactions[i + window_size]
max_sum = max(max_sum, window_sum)
return max_sum
# Test the function with sample data
transactions = [500, 200, 300, 100, 50, 400, 350, 200, 600, 800]
window_size = 7
print(f"The maximum transaction sum over any {window_size}-day window is: {max_transaction_sum(transactions, window_size)}")
Explanation of Code:
Line 2-5: If the number of transactions is less than the window size, we simply return the sum of all the transactions (since no sliding window can be formed).
Line 8-9: We start by calculating the sum of the first
window_size
elements (transactions[0] to transactions[6] for a 7-day window).Line 12-14: We slide the window from the beginning of the list to the end, updating the sum by subtracting the element that's going out of the window and adding the one that's coming into the window.
Line 16: Finally, we return the maximum sum encountered during the sliding process.
Practice Problem:
Problem:
A Nigerian bank tracks the number of failed transactions each day for a month (30 days). The bank wants to identify the maximum number of failed transactions over any 10-day sliding window. Can you implement this using the sliding window technique?
Input:
An array of integers where each integer represents the number of failed transactions on a given day.
Example Input:
failed_transactions = [10, 20, 15, 25, 30, 40, 35, 50, 60, 45, 30, 20, 15, 10, 25, 50, 40, 35, 30, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 110]
window_size = 10
Expected Output:
Find the maximum number of failed transactions in any 10-day sliding window.
Solution:
def max_failed_transactions(failed_transactions, window_size):
# Edge case: If the number of days is less than the window size, return the sum of all failed transactions
if len(failed_transactions) < window_size:
return sum(failed_transactions)
# Calculate the sum of the first window
window_sum = sum(failed_transactions[:window_size])
max_sum = window_sum
# Slide the window across the failed transactions array
for i in range(len(failed_transactions) - window_size):
window_sum = window_sum - failed_transactions[i] + failed_transactions[i + window_size]
max_sum = max(max_sum, window_sum)
return max_sum
# Test the function with sample data
failed_transactions = [10, 20, 15, 25, 30, 40, 35, 50, 60, 45, 30, 20, 15, 10, 25, 50, 40, 35, 30, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 110]
window_size = 10
print(f"The maximum number of failed transactions over any {window_size}-day window is: {max_failed_transactions(failed_transactions, window_size)}")
Summary:
The Sliding Window technique is effective for problems where you need to track a subset of data over a fixed or dynamic window and calculate values like sums, averages, or maximums/minimums.
In Nigerian banking, such techniques can be applied to track customer transaction patterns, identify potential fraudulent activity, or monitor operational metrics like failed transactions.
The above practice problem illustrates how the sliding window can be applied to detect patterns in a sequence of numbers, which in this case represents failed transactions.
Subscribe to my newsletter
Read articles from Ekemini Thompson directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by