Daily Net Flows per Wallet From the Transfers Table
Last updated: November 29, 2026
Quick Overview
Given a transfers table with wallet_id, coin, direction ('in' or 'out'), amount_base_units, fee_base_units, state and confirmed_at, write SQL that returns, for each wallet, coin and UTC day, total deposits, total withdrawals including fees, and the net flow, counting only confirmed transfers.
BitGo
November 29, 20260
0
0 solved
Given a transfers table with wallet_id, coin, direction ('in' or 'out'), amount_base_units, fee_base_units, state and confirmed_at, write SQL that returns, for each wallet, coin and UTC day, total deposits, total withdrawals including fees, and the net flow, counting only confirmed transfers.
Treasury, finance and client reporting at a custodian like BitGo depend on accurate daily flow numbers per wallet and asset. The screen checks clean aggregation, correct handling of fees and states, and awareness that amounts must stay in integer base units per coin.
What the Interviewer Expects
- Filters to confirmed transfers only
- Uses conditional aggregation rather than self-joins
- Adds fees to outflows because the wallet pays them
- Groups by coin as well as wallet and never sums across coins
- Buckets by UTC day explicitly
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you show amounts in human units given a coins table with decimals?
- What if a transfer is confirmed and later marked reorged?
- How would you include days with zero activity?
- Where do fee-only transactions (like account consolidations) belong?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
One row per transfer. amount_base_units is a positive integer in the coin's smallest unit. fee_base_units is non-null only for outgoing transfers paid by the wallet. state includes 'unconfirmed'...
Approach
Filter to state = 'confirmed', bucket confirmed_at to a day, and use SUM(CASE ...) to split in and out in one pass. Net is inflows minus outflows minus fees. Keep everything in integer base unit...