DDSA Solutions

2043. Simple Bank System

Advertisement

Approach

Store balances in array; validate account indices on each operation.

Key Techniques

Array

Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.

Hash Table

Hash tables provide O(1) average-case lookup, insert, and delete. They are the go-to tool for counting frequencies, detecting complements (Two Sum pattern), and caching seen values. In C#, use Dictionary<K,V> for maps and HashSet<T> for membership checks.

Design

Design problems ask you to implement a data structure or system with specific API contracts. Common designs: LRU Cache (HashMap + doubly linked list), LFU Cache, Min Stack, Iterator, and streaming median. Focus on the time complexity required for each operation.

2043.cs
C#
// Approach: Store balances in array; validate account indices on each operation.
// Time: O(1) per op Space: O(n)

public class Bank
{
    // Array to store account balances
    private long[] accountBalances;
    // Total number of accounts in the bank
    private int numberOfAccounts;

    /// <summary>
    /// Constructor to initialize the bank with initial account balances
    /// </summary>
    /// <param name="balance">Array of initial balances for each account</param>
    public Bank(long[] balance)
    {
        this.accountBalances = balance;
        this.numberOfAccounts = balance.Length;
    }

    /// <summary>
    /// Transfer money from one account to another
    /// </summary>
    /// <param name="account1">Source account number (1-indexed)</param>
    /// <param name="account2">Destination account number (1-indexed)</param>
    /// <param name="money">Amount of money to transfer</param>
    /// <returns>true if transfer is successful, false otherwise</returns>
    public bool Transfer(int account1, int account2, long money)
    {
        // Validate both account numbers exist and source account has sufficient balance
        if (account1 > numberOfAccounts || account2 > numberOfAccounts ||
            accountBalances[account1 - 1] < money)
            return false;

        // Deduct money from source account
        accountBalances[account1 - 1] -= money;
        // Add money to destination account
        accountBalances[account2 - 1] += money;

        return true;
    }

    /// <summary>
    /// Deposit money into an account
    /// </summary>
    /// <param name="account">Account number (1-indexed)</param>
    /// <param name="money">Amount of money to deposit</param>
    /// <returns>true if deposit is successful, false otherwise</returns>
    public bool Deposit(int account, long money)
    {
        // Validate account number exists
        if (account > numberOfAccounts)
            return false;

        // Add money to the account
        accountBalances[account - 1] += money;

        return true;
    }

    /// <summary>
    /// Withdraw money from an account
    /// </summary>
    /// <param name="account">Account number (1-indexed)</param>
    /// <param name="money">Amount of money to withdraw</param>
    /// <returns>true if withdrawal is successful, false otherwise</returns>
    public bool Withdraw(int account, long money)
    {
        // Validate account number exists and has sufficient balance
        if (account > numberOfAccounts || accountBalances[account - 1] < money)
            return false;

        // Deduct money from the account
        accountBalances[account - 1] -= money;

        return true;
    }
}

/**
 * Your Bank object will be instantiated and called as such:
 * Bank obj = new Bank(balance);
 * bool param_1 = obj.Transfer(account1,account2,money);
 * bool param_2 = obj.Deposit(account,money);
 * bool param_3 = obj.Withdraw(account,money);
 */
Advertisement
Was this solution helpful?