PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of data structures, state management, transactional correctness and edge-case handling (including invalid or negative amounts and overdraft prevention) when implementing deposit, withdraw, and transfer operations.

  • Medium
  • Instacart
  • Coding & Algorithms
  • Software Engineer

Implement a banking operations system

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Implement a simple banking system that manages multiple accounts with initial balances. Support operations: deposit(accountId, amount), withdraw(accountId, amount), and transfer(fromId, toId, amount). Reject operations that would overdraw an account or reference a non-existent account; for each operation return whether it succeeded. Provide a function that applies a sequence of operations and returns the final balances. Discuss data structures, edge cases (e.g., zero/negative amounts, large account counts), and time/space complexity. If operations arrive as a stream, explain how you would process them efficiently.

Quick Answer: This question evaluates understanding of data structures, state management, transactional correctness and edge-case handling (including invalid or negative amounts and overdraft prevention) when implementing deposit, withdraw, and transfer operations.

Implement a simple banking system that manages multiple accounts, each with an initial balance. You are given: - `initial_balances`: a mapping from account id (string) to its starting balance (integer, >= 0). - `operations`: a list of operations to apply in order. Each operation is one of: - `['deposit', accountId, amount]` - `['withdraw', accountId, amount]` - `['transfer', fromId, toId, amount]` Rules for an operation to SUCCEED: - Every referenced account must already exist in the bank. - `amount` must be strictly positive (reject zero and negative amounts). - A `withdraw` or `transfer` must not overdraw the source account (balance must be >= amount). - A `transfer` to the same account (`fromId == toId`) is rejected. Apply the operations in order. A rejected operation leaves all balances unchanged. Successful operations mutate balances and (for transfer) move funds from source to destination. Return a list `[results, final_balances]` where: - `results` is a list of booleans, one per operation, indicating whether that operation succeeded. - `final_balances` is the mapping of account id to its final balance after all operations have been applied. Example: - initial_balances = {'A': 100, 'B': 50} - operations = [['deposit','A',50], ['withdraw','B',20], ['transfer','A','B',100]] - returns [[True, True, True], {'A': 50, 'B': 130}]

Constraints

  • 0 <= number of accounts <= 10^5
  • 0 <= number of operations <= 10^5
  • Initial balances are non-negative integers.
  • amount may be any integer; only strictly positive amounts can produce a successful operation.
  • Account ids are unique strings.
  • A rejected operation must leave all balances unchanged.

Examples

Input: ({'A': 100, 'B': 50}, [['deposit', 'A', 50], ['withdraw', 'B', 20], ['transfer', 'A', 'B', 100]])

Expected Output: [[True, True, True], {'A': 50, 'B': 130}]

Explanation: Deposit 50 into A (100->150 then transfer takes 100), withdraw 20 from B (50->30), transfer 100 from A to B succeeds since A has 150. Final: A=50, B=130.

Input: ({'A': 100}, [['withdraw', 'A', 200], ['deposit', 'A', 0], ['deposit', 'A', -10], ['transfer', 'A', 'C', 10]])

Expected Output: [[False, False, False, False], {'A': 100}]

Explanation: Overdraw (200 > 100) rejected; zero amount rejected; negative amount rejected; transfer to non-existent account C rejected. Balance of A is unchanged at 100.

Input: ({}, [['deposit', 'X', 10]])

Expected Output: [[False], {}]

Explanation: Account X does not exist in an empty bank, so the deposit is rejected and the bank stays empty.

Input: ({'A': 0, 'B': 0}, [])

Expected Output: [[], {'A': 0, 'B': 0}]

Explanation: No operations to apply; results is empty and balances are returned unchanged.

Input: ({'A': 100, 'B': 100}, [['transfer', 'A', 'A', 50], ['withdraw', 'A', 100], ['transfer', 'A', 'B', 1]])

Expected Output: [[False, True, False], {'A': 0, 'B': 100}]

Explanation: Self-transfer A->A is rejected; withdraw 100 from A drains it to 0; transfer of 1 from A now fails because A has insufficient funds. Final: A=0, B=100.

Hints

  1. Use a hash map (dict) keyed by account id for O(1) existence checks and balance updates.
  2. Validate every precondition BEFORE mutating any balance, so a rejected operation leaves state untouched. For a transfer, check both accounts exist, the amount is positive, source != destination, and the source can cover the amount before moving any funds.
  3. Treat zero and negative amounts as invalid for every operation type. For a stream, process each operation in O(1) as it arrives — no need to buffer; the same per-operation validation logic applies online.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Fix the Broken Search Filter in a Book Catalog API - Instacart (medium)
  • Find Largest Adjacent Stock Price Change - Instacart (medium)
  • Implement an In-Memory File Storage System - Instacart (medium)
  • Decode an encoded string - Instacart (medium)
  • Evaluate an arithmetic expression - Instacart (medium)