r/Hack2Hire • u/Hack2hire • Oct 28 '26
Onsite Coinbase Onsite: Crypto Block Mining
Problem A miner packs transactions from a pending pool (the mempool) into a block with fixed capacity blockSize. The miner collects the fee of every included transaction, so the goal is to maximize total fees without exceeding the size limit.
You are given three parallel arrays transactionIds, fees, and sizes, each of length N. Implement mineBlock to return the IDs of the chosen transactions, in any order.
Transactions are prioritized by fee-to-size ratio, fees[i] / sizes[i]; a higher ratio is preferred. Ties are broken in this order:
- Higher fee.
- Smaller size.
- Lexicographically smaller ID.
Constraints: 1 ≤ N ≤ 105, 1 ≤ sizes[i] ≤ blockSize ≤ 109, 0 ≤ fees[i] ≤ 106.
Follow-up Each transaction now has an optional parentId pointing to another transaction in the mempool. A parent must be mined in the same block, before its child. A parent can have several children, a child has at most one parent, and there are no cycles. An empty parentId means no prerequisite.
This is Child-Pays-For-Parent (CPFP). A transaction's priority is computed from the combined fee and combined size of the transaction plus all its unmined ancestors, up to the root or the nearest already-mined ancestor. Once a parent is mined, its cost is paid for, and its children are evaluated using only their remaining unmined ancestors.
Extend mineBlock with a parentIds parameter and return the selected IDs, in any order.
Example Input: transactionIds = ["tx1","tx2","tx3","tx4"], fees = [50,60,30,40], sizes = [50,30,20,40], blockSize = 100 Output: ["tx2","tx3","tx1"]
Explanation:
tx2has ratio 2.0,tx3has 1.5, andtx1andtx4both have 1.0.- Taking
tx2(size 30),tx3(size 20), andtx1(size 50) fills exactly 100 units for a total fee of 140. tx1andtx4tie on ratio, so the higher fee,tx1, wins.
Suggested Approach
- Pair each ID with its fee and size so each transaction can be compared as one object.
- Sort from highest to lowest fee-to-size ratio. Compare two ratios by cross-multiplication,
fee_a × size_bagainstfee_b × size_a, to avoid floating-point error. Apply the tie-breakers in the stated order. - Walk the sorted list and include a transaction if it fits in the remaining capacity; skip it otherwise.
- For the CPFP follow-up, rank packages instead of single transactions. A package is a transaction plus its unmined ancestors, with the summed fee and size, and it is a candidate only if it fits in the remaining space.
- Repeat: pick the valid package with the best ratio using the same comparison rules, mine its ancestors first, reduce the remaining space, and mark them mined. Re-evaluate every round, because mining a parent shrinks the package of its other children and usually improves their ratios.
Time & Space Complexity
- Time: O(N log N) for the base problem, from sorting. The CPFP follow-up is O(N² × D), where D is the average ancestor chain depth. N is the number of transactions.
- Space: O(N) for the sorted list and the output.
Targeting Coinbase interviews? We track their most-asked question patterns at Hack2Hire. Practice this question here → Practice Question Here
Join the community to see more interview experiences from real candidates → Hack2Hire Forum
Compiled from publicly available platforms and community-shared experiences.