KatInterview

Packing a Fixed-Size Block from a Large Mempool with CPFP

Greedy & IntervalsModel answer
MediumModel answerGreedy & Intervalsgreedyknapsackdependency-tracking

Last updated

Design a fee-maximizing transaction selector for a size-capped block, extend it to parent/child dependencies with CPFP scoring, and derive a minimum child fee.

Context

A miner builds a candidate block from a mempool — an unordered collection of pending transactions. The block has a fixed capacity B = 100 (in the same units as transaction size), and the miner keeps every fee from every transaction it includes. Each transaction has three fields: id, fee (a non-negative integer), and size.

The mempool may be enormous, so the interviewer explicitly prefers a fast heuristic over an exact optimizer, even if the heuristic leaves a little fee on the table.

Part 1 — Fill one block

Implement mine_block(mempool) -> list[Transaction] returning the transactions to mine, whose sizes sum to at most B, maximizing total collected fee subject to the stated speed preference. State your time and space complexity.

Cover: the selection rule and why it matches a block whose scarce resource is space; what you do when the next candidate no longer fits; inputs where the rule behaves poorly; and how your cost compares to an exact method on a very large mempool.

Part 2 — Parents and Child-Pays-For-Parent

Add a parent_id field. A transaction's parent must be mined in the same block and appear earlier in the returned list; if parent_id is absent, its parent was already mined. A parent may have multiple children; a parent_id pointing at a transaction not in the mempool should be treated as already mined (confirm with the interviewer). No cycles exist.

Child-Pays-For-Parent (CPFP) means a child may overpay so the miner is rewarded for pulling in its low-fee parent — possibly a chain of ancestors. Rework mine_block so that:

  • selected transactions fit in B;
  • every selected transaction's parent is also selected;
  • parents are listed before children.

Cover: a score that reflects the unmined ancestors a transaction drags along; how to keep scores correct once an ancestor was already pulled in by a different child; and the cost on a large mempool.

Part 3 — Optional: the minimum fee a child must pay

Implement min_fee(mempool, parent_id, child_size) -> uint64: the fee a new child of size child_size should pay so that it and its parent both land in the next block that your Part 2 mine_block would build from the mempool plus this child.

Cover: a precise definition of "minimum" tied to your selection rule; an algorithm to find it with exact integer arithmetic; and cases where every fee works (e.g., parent and child fit and the block would otherwise be empty) or none can (parent and child together exceed B, or an ancestor is missing from the mempool).

Hints

Hint 1

When space is the bottleneck and each item can be taken at most once, the natural fast heuristic is to rank candidates by fee per unit size rather than raw fee.

Hint 2

For a first fit, sort by ratio and sweep; the leftover gap at the end is where a more careful re-pick of one item could recover fee.

Hint 3

For CPFP, a transaction's effective score is the total fee of itself plus all not-yet-selected ancestors divided by the total size of that same package; recompute as the block fills.

Hint 4

For min_fee, binary search the child fee (monotone in whether the pair is selected) or solve the boundary inequality directly with integer cross-multiplication.