KatInterview

Bird Physics Tick and Fee-Ratio Block Packing

Greedy & IntervalsModel answer
HardModel answerGreedy & Intervalsgreedytopological-sortcycle-detection

Last updated

Two coding tasks: a one-tick bird movement function with inclusive bounds, and a greedy block builder that scores transactions together with their unconfirmed ancestors.

Solve both of the following tasks and explain your reasoning.

Task A: One simulation tick for a flying character

A side-scrolling game keeps a character at some vertical position. The ceiling is at y = 0, the floor is at y = height, and a positive velocity moves the character downward.

Write:

update_bird(y, velocity, jump_pressed, height, gravity, jump_velocity) -> (new_y, new_velocity, alive)

Per tick:

  1. If jump_pressed is true, the current velocity is replaced by jump_velocity (usually negative). Otherwise the current velocity is kept.
  2. Gravity is added to that velocity, giving new_velocity.
  3. The position moves by the new velocity: new_y = y + new_velocity.
  4. The character is alive only if 0 <= new_y <= height (both ends inclusive).
  5. Return (new_y, new_velocity, alive).

Say how your function behaves when a jump happens close to the ceiling, when the character falls past the floor, when it lands exactly on a boundary, and when gravity is zero.

Task B: Assembling a block from dependent transactions

A simplified miner has a pool of pending transactions. Each one has:

  • id: a unique string
  • fee: an integer
  • size: an integer number of bytes
  • parents: IDs of transactions that must appear earlier in the same block if this one is included

Given max_size, return an ordered list of transaction IDs for the block, along with the total fee and total size.

Rules:

  1. A transaction may be included only if every parent is also included and placed before it.
  2. The summed size of the block must not exceed max_size.
  3. Do not attempt the optimal knapsack. Use this greedy procedure:
    • For each transaction not yet in the block, form its package: the transaction plus every ancestor not yet in the block.
    • The package fee and package size are the sums over those not-yet-included transactions.
    • Repeatedly pick the package with the highest package_fee / package_size among those that fit in the remaining space, and append its members in dependency order.
  4. If the dependency graph has a cycle, report an error.
  5. Return the ordered IDs, total fee and total size.

Finally, analyse the time complexity of your approach and describe how you would cache or incrementally maintain package data for a very large pool.

Hints

Hint 1

Task A is a direct translation of the rule order: choose the base velocity, add gravity, move by the updated velocity, then test the inclusive bounds. Do not clamp the returned position.

Hint 2

For Task B, run a topological sort once up front. It detects cycles, and its ordering gives you a ready-made way to sort any package into dependency order. Compare ratios by cross-multiplying instead of dividing.

Hint 3

A package that does not fit now can become smaller after some of its ancestors are included, so scores change over time. For the scalable version, ask which transactions' packages change after a package is added (only the descendants) and how a max-heap with lazy invalidation could exploit that.