Frontend School

Nykaa Transaction Log Processor

DSA
hard

Nykaa Transaction Log Processor

Hard
Queue
Deque
Sliding Window
Data Structure Design

Overview

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.

What you need to do / Task

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:

  1. A constructor TransactionLogProcessor(maxTransactionsCount: number, maxSumCapacity: number) that initializes the log with the given limits.
  2. A method addTransaction(value: number): number that processes a new transaction. When a new transaction value arrives:
    • If the 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.
    • Otherwise, before adding the new 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.
    • After ensuring sufficient capacity (by removing old transactions if necessary), the new value is added to the log.
    • Finally, return the current cumulative sum of all transactions currently in the log.

Input & output

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.
  • Returns: A non-negative integer representing the cumulative sum of all transactions currently present in the log after processing the new value.

Clarifications

  • All transaction 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

1

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.

  1. addTransaction(2): Log: [2], Sum: 2. Returns 2.
  2. addTransaction(3): Log: [2,3], Sum: 5. Returns 5.
  3. addTransaction(4): Log: [2,3,4], Sum: 9. Returns 9. (Count 3 <= 3, Sum 9 <= 10)
  4. addTransaction(5): Proposed: Log size 4 (>3), Sum 14 (>10). Remove 2. Log: [3,4], Sum: 7. Proposed: Log size 3 (<=3), Sum 12 (>10). Remove 3. Log: [4], Sum: 4. Proposed: Log size 2 (<=3), Sum 9 (<=10). Add 5. Log: [4,5], Sum: 9. Returns 9.
  5. addTransaction(1): Proposed: Log size 3 (<=3), Sum 10 (<=10). Add 1. Log: [4,5,1], Sum: 10. Returns 10.
2

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.

  1. addTransaction(3): Log: [3], Sum: 3. Returns 3.
  2. addTransaction(4): Proposed: Log size 2 (<=2), Sum 7 (>5). Remove 3. Log: [], Sum: 0. Proposed: Log size 1 (<=2), Sum 4 (<=5). Add 4. Log: [4], Sum: 4. Returns 4.
  3. addTransaction(7): Value 7 > maxSumCapacity 5. Cannot add. Log remains [4], Sum: 4. Returns 4.
3

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.

  1. addTransaction(0): Log: [0], Sum: 0. Returns 0.
  2. addTransaction(5): Value 5 > maxSumCapacity 0. Cannot add. Log remains [0], Sum: 0. Returns 0.
  3. addTransaction(0): Proposed: Log size 2 (>1), Sum 0 (<=0). Remove 0. Log: [], Sum: 0. Proposed: Log size 1 (<=1), Sum 0 (<=0). Add 0. Log: [0], Sum: 0. Returns 0.

Constraints

Constraints & assumptions

  • 0 <= maxTransactionsCount <= 10^5
  • 0 <= maxSumCapacity <= 10^9
  • 0 <= value <= 10^9
  • The total number of addTransaction calls will not exceed 10^5.
  • The cumulative sum of values can fit within a standard 64-bit integer type (e.g., JavaScript number or TypeScript number supports integers up to 2^53 - 1, which is sufficient for 10^9 * 10^5 = 10^{14}).

What interviewers evaluate

  • Correctly manages both transaction count and cumulative sum constraints simultaneously.
  • Achieves efficient addTransaction operations with amortized O(1) time complexity.
  • Demonstrates robust handling of various edge cases, including empty logs, zero capacities, and transactions that alone exceed capacity.
  • Produces clean, readable, and well-structured code with appropriate data structure choices.

Loading editor...