KatInterview

Build a Bank, Validate Passwords, and Evaluate Expressions

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-mapstackparsing

Last updated

Design a mini banking system, write a password strength checker with generator, and parse arithmetic expressions with full operator precedence.

Three-Part Coding Challenge

You have 90 minutes to complete three independent problems. For each one, state your assumptions out loud, handle invalid input gracefully, and aim for clean, testable code. The interviewer is as interested in how you reason through edge cases as in whether the final output is correct.


Part 1: Mini Banking System

Build a class that manages accounts and supports four operations. Decide on and declare your contracts before writing any code.

Operations to support:

  • create_account(timestamp, account_id) — registers a new account with a zero balance; returns False if the ID is already taken.
  • deposit(timestamp, account_id, amount) — adds funds and returns the new balance; returns None for an unknown account or a non-positive amount.
  • transfer(timestamp, source_id, dest_id, amount) — moves funds between two accounts and returns the new source balance; returns None if either account is missing, if source and destination are the same, if the amount is non-positive, or if the source has insufficient funds.
  • top_spenders(timestamp, n) — returns up to n account IDs formatted as "id(total_spent)", ordered by total outgoing money descending, with ties broken alphabetically by ID.

Edge cases to consider: duplicate account creation, self-transfers, exact-balance transfers, n exceeding the number of accounts, and ties in the ranking.


Part 2: Password Strength Checker and Generator

Validator: given a string, return a list of all violated rules (not just a pass/fail boolean). A password passes if it:

  • is between 8 and 64 characters long,
  • contains at least one lowercase letter, one uppercase letter, one digit, and one character from a defined set of special characters,
  • contains no whitespace,
  • has no character appearing three or more times consecutively.

Generator: produce a random password of a given length (default 16) that satisfies all rules. Use a cryptographically secure source of randomness.

Edge cases to consider: empty strings, exactly 8 or 64 characters, a triple-repeat buried inside an otherwise valid password, and Unicode input.


Part 3: Arithmetic Expression Evaluator

Write a function that takes a string representing an arithmetic expression and returns its integer result.

Requirements:

  • Support +, -, *, / and parentheses.
  • Handle unary + and - (e.g. "-(2+3)", "--4").
  • Ignore whitespace.
  • Integer division truncates toward zero (so 7 / -2 = -3, not -4).
  • Raise a clear error on division by zero, unbalanced parentheses, or invalid tokens.

Edge cases to consider: chained unary operators, left-associativity for subtraction and division, and trailing or unconsumed tokens.

Hints

Hint 1

For the banking system, a failed transfer must leave both balances completely unchanged — check all preconditions before touching any state. Track outgoing totals in a separate dict so ranking doesn't require scanning transactions.

Hint 2

For password validation, check every rule independently and collect all failures before returning — a bare True/False is less useful. For generation, seed the result with one character from each required class before filling the rest, then shuffle; use `secrets`, not `random`.

Hint 3

For expression evaluation, a recursive-descent parser maps cleanly onto operator precedence: one function per grammar level (expr → term → factor). Truncation toward zero is `int(a / b)` in Python, not `a // b`, which floors toward negative infinity.