Frontend School

Scoped Transaction Balances

DSA
medium

Scoped Transaction Balances

Medium
Stack
Parsing
Financial
Data Structures

Overview

At Razorpay, managing financial transactions accurately is paramount. In complex financial systems, transactions often occur within nested contexts or 'scopes', where a set of operations might contribute to an intermediate balance before being consolidated into a parent account. For example, a batch payment system might group multiple sub-transactions, or a refund process might temporarily isolate funds. This problem challenges you to process a series of financial operations that include starting and ending such nested scopes, ultimately calculating the final consolidated balance.

What you need to do / Task

Your task is to implement a function calculateFinalBalance that processes a list of financial operations and returns the final consolidated balance. The operations can modify the current active balance or manage nested scopes.

Handle the following operations:

  1. DEPOSIT <amount>: Adds <amount> to the current active balance.
  2. WITHDRAW <amount>: Subtracts <amount> from the current active balance.
  3. BEGIN_SCOPE: Initiates a new nested transaction scope. The current active balance is pushed onto a stack, and the new scope starts with an active balance of 0.
  4. END_SCOPE: Concludes the current nested transaction scope. The accumulated balance of the just-finished scope is added to the parent scope's balance (which was previously pushed onto the stack). The parent scope's balance then becomes the new current active balance.

The initial active balance is 0.

Input & output

  • Input: operations: string[]
    • An array of strings, each representing a financial operation. Each string will be in one of the formats described above (e.g., "DEPOSIT 100", "BEGIN_SCOPE").
  • Output: number
    • A single integer representing the final consolidated balance after processing all operations.

Clarifications

  • Amounts for DEPOSIT and WITHDRAW will always be positive integers.
  • The balance can become negative during processing.
  • You can assume that END_SCOPE will always be called only when there is a corresponding BEGIN_SCOPE (i.e., the input will always represent a valid nesting structure).

Examples

1

Example 1

Input:

operations = ["DEPOSIT 100", "WITHDRAW 30", "DEPOSIT 50"]

Output:

120

Note:

Initial balance: 0

  1. DEPOSIT 100 -> balance = 100
  2. WITHDRAW 30 -> balance = 70
  3. DEPOSIT 50 -> balance = 120 Final balance: 120
2

Example 2

Input:

operations = ["DEPOSIT 100", "BEGIN_SCOPE", "DEPOSIT 50", "END_SCOPE", "WITHDRAW 20"]

Output:

130

Note:

Initial balance: 0

  1. DEPOSIT 100 -> balance = 100
  2. BEGIN_SCOPE -> Stack: [100], Current balance: 0
  3. DEPOSIT 50 -> Current balance: 50
  4. END_SCOPE -> Pop 100. Parent balance becomes 100 + 50 = 150. Current balance: 150
  5. WITHDRAW 20 -> Current balance: 130 Final balance: 130
3

Example 3

Input:

operations = ["DEPOSIT 10", "BEGIN_SCOPE", "DEPOSIT 5", "BEGIN_SCOPE", "DEPOSIT 2", "END_SCOPE", "END_SCOPE", "DEPOSIT 1"]

Output:

18

Note:

Initial balance: 0

  1. DEPOSIT 10 -> balance = 10
  2. BEGIN_SCOPE -> Stack: [10], Current balance: 0
  3. DEPOSIT 5 -> Current balance: 5
  4. BEGIN_SCOPE -> Stack: [10, 5], Current balance: 0
  5. DEPOSIT 2 -> Current balance: 2
  6. END_SCOPE -> Pop 5. Parent balance becomes 5 + 2 = 7. Current balance: 7. Stack: [10]
  7. END_SCOPE -> Pop 10. Parent balance becomes 10 + 7 = 17. Current balance: 17. Stack: []
  8. DEPOSIT 1 -> Current balance: 18 Final balance: 18

Constraints

Constraints & assumptions

  • 1 <= operations.length <= 10^5
  • 1 <= amount <= 10^9 for DEPOSIT and WITHDRAW operations.
  • Intermediate and final balances will fit within a standard 64-bit integer type (e.g., JavaScript's number type, which handles up to 2^53 - 1 precisely).
  • All operation strings are well-formed and valid.

What interviewers evaluate

  • Correctness in handling nested transaction scopes and balance consolidation.
  • Efficient use of appropriate data structures (specifically, a stack) for scope management.
  • Clarity, readability, and maintainability of the code.
  • Optimal time complexity, aiming for O(N) where N is the number of operations.

Loading editor...