KatInterview

Whole-Share Rebalancing for a Fractional Stock Ledger

Math & Bit ManipulationAuto-graded · 23 test cases
MediumAuto-gradedMath & Bit Manipulationsimulationstring-parsinginteger-arithmetic

Last updated

Simulate customer buy and sell orders against fractional inventory, trading only whole shares with the market, and emit ordered transfer logs and final balances.

A brokerage holds a stock inventory per symbol, measured in hundredths of a share. For example, 50 means 0.50 shares. Customers buy from it and sell to it, but the outside market only accepts whole shares, so every market trade must be a multiple of 100 hundredths. After every order, each symbol's inventory must be in the range 0 <= q < 100.

Implement rebalance_ledger(orders, holdings). It returns a list of exactly two lists:

  1. The final inventory strings, in the same order as holdings.
  2. The ordered list of transfer log strings.

Input format

  • holdings: a list of strings SYMBOL/quantity. The quantity is an integer number of hundredths of a share in [0, 99]. Symbols are unique, nonempty, and made of ASCII letters, digits, and underscores.
  • orders: a list of strings SYMBOL/SIDE/amount/price, processed in the given order.
    • SIDE is B (the customer buys from the brokerage) or S (the customer sells to the brokerage).
    • amount is either a plain positive integer, meaning that many hundredths of a share, or a positive integer prefixed with $, meaning that many cents of money.
    • price is a positive integer number of cents per whole share.
    • For a $ order, the share quantity in hundredths is cents * 100 / price. Test data guarantees this is always an exact integer, so no rounding is needed.
    • Every order symbol appears in holdings.

Processing rules

Let q be the symbol's current inventory and a the order quantity in hundredths.

Customer buy (B)

  1. If q < a, the brokerage first buys from the market the smallest multiple of 100 that makes q + m >= a. Log it as a market buy only if m > 0.
  2. Then it delivers all a to the customer, logged as a customer sell.
  3. New inventory is q + m - a.

Customer sell (S)

  1. The brokerage first receives a from the customer, logged as a customer buy. Inventory becomes q + a.
  2. It then sells to the market the largest multiple of 100 not exceeding that inventory. Log it only if that amount is greater than 0.
  3. New inventory is the remainder.

Log format

Each log entry is SYMBOL/quantity/side/contra:

  • quantity is a positive integer in hundredths. Never emit a zero-quantity log.
  • side is B or S from the brokerage's perspective (what the brokerage did).
  • contra is MARKET or CUSTOMER.

Per order, logs appear in this order:

  • Buy order: optional SYMBOL/m/B/MARKET, then SYMBOL/a/S/CUSTOMER.
  • Sell order: SYMBOL/a/B/CUSTOMER, then optional SYMBOL/m/S/MARKET.

Final inventory strings use the form SYMBOL/quantity. Symbols with no orders still appear, unchanged.

This models quantity accounting only; prices, fees, and settlement are not simulated.

Constraints

  • At most 100000 orders and 1000 symbols.
  • All numeric values in the input are at most 1000000, and order quantities are positive.
  • Use integer arithmetic only; do not use floating point.
  • $ orders always convert to a whole number of hundredths.

Examples

Example 1

Input: rebalance_ledger(['ABC/B/150/1000', 'ABC/B/40/1000', 'ABC/S/75/1000'], ['ABC/0', 'XYZ/75'])

Output: [['ABC/85', 'XYZ/75'], ['ABC/200/B/MARKET', 'ABC/150/S/CUSTOMER', 'ABC/40/S/CUSTOMER', 'ABC/75/B/CUSTOMER']]

Example 2

Input: rebalance_ledger(['ABC/B/50/1000'], ['ABC/20'])

Output: [['ABC/70'], ['ABC/100/B/MARKET', 'ABC/50/S/CUSTOMER']]

Example 3

Input: rebalance_ledger(['ABC/S/50/1000'], ['ABC/75'])

Output: [['ABC/25'], ['ABC/50/B/CUSTOMER', 'ABC/100/S/MARKET']]

Hints

Hint 1

Keep everything as integers in hundredths of a share. Parse the `$` prefix separately and convert cents to hundredths with `cents * 100 // price`.

Hint 2

For a buy, only the shortfall `a - q` matters. The smallest multiple of 100 covering a positive shortfall is a ceiling division, and if there is no shortfall the market trade is zero.

Hint 3

For a sell, add the shares first, then sell off `(q // 100) * 100`. Only append market logs when the amount is positive, and keep a separate list of symbols to preserve the final output order.