Nykaa Transaction Log Processor
Nykaa's fraud detection and analytics systems rely on real-time monitoring of customer transaction patterns. To efficiently manage the vast amount of data and focus on recent activity, we need to maintain a bounded log of transactions for each customer. This log must adhere to strict limits on both the total number of transactions and their cumulative monetary value.
You are tasked with implementing a TransactionLogProcessor class. This class will simulate a bounded transaction log, managing incoming transaction values according to two crucial constraints: maxTransactionsCount and maxSumCapacity.
Your implementation must provide:
TransactionLogProcessor(maxTransactionsCount: number, maxSumCapacity: number) that initializes the log with the given limits.addTransaction(value: number): number that processes a new transaction. When a new transaction value arrives:
value itself is strictly greater than maxSumCapacity, it cannot be added to the log, as it alone would violate the sum constraint. In this case, the log remains unchanged, and you should return its current cumulative sum.value, if its inclusion would cause the log to exceed maxTransactionsCount (i.e., current_log_size + 1 > maxTransactionsCount) or maxSumCapacity (i.e., current_sum + value > maxSumCapacity), you must remove the oldest transactions from the front of the log until both constraints are satisfied for the potential inclusion of the new value. This removal process must continue as long as either constraint is violated.value is added to the log.Class Name: TransactionLogProcessor
Constructor: TransactionLogProcessor(maxTransactionsCount: number, maxSumCapacity: number)
maxTransactionsCount: A non-negative integer representing the maximum number of transactions allowed in the log.maxSumCapacity: A non-negative integer representing the maximum cumulative sum of transaction values allowed in the log.Method: addTransaction(value: number): number
value: A non-negative integer representing the value of the new transaction to be added.value.values are non-negative.maxTransactionsCount and maxSumCapacity are non-negative integers. If maxTransactionsCount is 0, no transactions can ever be added to the log. If maxSumCapacity is 0, only transactions with value 0 can potentially be added (if maxTransactionsCount allows).Examples
Example 1
Input:
operations = ["TransactionLogProcessor","addTransaction","addTransaction","addTransaction","addTransaction","addTransaction"], params = [[3,10],[2],[3],[4],[5],[1]]
Output:
[null,2,5,9,9,10]
Note:
Initialize with maxTransactionsCount=3, maxSumCapacity=10.
Example 2
Input:
operations = ["TransactionLogProcessor","addTransaction","addTransaction","addTransaction"], params = [[2,5],[3],[4],[7]]
Output:
[null,3,4,4]
Note:
Initialize with maxTransactionsCount=2, maxSumCapacity=5.
Example 3
Input:
operations = ["TransactionLogProcessor","addTransaction","addTransaction","addTransaction"], params = [[1,0],[0],[5],[0]]
Output:
[null,0,0,0]
Note:
Initialize with maxTransactionsCount=1, maxSumCapacity=0.
Constraints
0 <= maxTransactionsCount <= 10^50 <= maxSumCapacity <= 10^90 <= value <= 10^9addTransaction calls will not exceed 10^5.number or TypeScript number supports integers up to 2^53 - 1, which is sufficient for 10^9 * 10^5 = 10^{14}).addTransaction operations with amortized O(1) time complexity.Loading editor...