Implement Game Physics and Block Mining
CoinbaseSoftware Engineer · OnsiteLast updated
This question evaluates implementation skills in deterministic game physics (discrete position and velocity updates with boundary and edge-case handling) alongside dependency-aware greedy selection for block construction, testing graph dependency resolution, greedy heuristics, and complexity analysis in the Coding & Algorithms domain, and is commonly asked to verify correctness under constraints, handling of boundary conditions, and trade-offs between optimality and performance. Level of abstraction spans both conceptual understanding (kinematic update rules, cycle detection, fee/size ratio reasoning) and practical application (implementing the tick update and returning a topologically ordered block while considering caching and scalability for large mempools).
You are given two independent coding tasks from the same interview category. Solve both.
Task A: Flappy-Bird-Style Jump Boundary Function
Implement a function for a simple side-scrolling game that updates a bird's vertical position for one simulation tick and reports whether the bird is still inside the playable area.
The game uses the following coordinate system:
y = 0is the ceiling.y = heightis the floor.- Positive velocity moves the bird downward.
Function signature:
update_bird(y, velocity, jump_pressed, height, gravity, jump_velocity) -> (new_y, new_velocity, alive)
Rules:
- If
jump_pressedis true, set the bird's velocity tojump_velocity.jump_velocitymay be negative. - Otherwise, keep the current velocity.
- Apply gravity to the velocity for this tick:
new_velocity = velocity_after_jump + gravity. - Update position:
new_y = y + new_velocity. - The bird is alive only if
0 <= new_y <= height. - Return the new position, new velocity, and alive status.
Handle edge cases such as jumps near the ceiling, falling below the floor, and zero gravity.
Task B: Build a Cryptocurrency Block with Transaction Dependencies
You are building a simplified block-mining simulator. Each pending transaction has:
id: unique stringfee: integer fee paid by the transactionsize: integer size in bytesparents: list of transaction IDs that must appear earlier in the same block if this transaction is included
Given a maximum block size max_size, choose an ordered list of transaction IDs to include in the block.
Rules:
- A transaction can be included only if all of its parent transactions are also included earlier in the returned order.
- The total size of all included transactions must be at most
max_size. - Use the following greedy strategy rather than solving the globally optimal knapsack problem:
- For each not-yet-included transaction, compute the package consisting of that transaction plus all missing ancestors required to include it.
- The package fee is the sum of fees of missing transactions in the package.
- The package size is the sum of sizes of missing transactions in the package.
- Repeatedly choose the valid package with the highest
package_fee / package_sizeratio that fits in the remaining block size. - Add the package transactions in dependency order.
- If the dependency graph contains a cycle, report an error.
- Return the ordered transaction IDs, total fee, and total size.
Discuss the time complexity of your implementation and how you would cache ancestor-package computations for large mempools.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.