PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates discrete-time physics simulation, real-time decision-making under constraints, collision detection, and deterministic control-policy design that prioritizes survival.

  • Coinbase
  • Coding & Algorithms
  • Software Engineer

Implement a Flappy Bird Jump Agent

Company: Coinbase

Role: Software Engineer

Category: Coding & Algorithms

Interview Round: Technical Screen

Implement a function `should_jump(state) -> bool` for a simplified Flappy Bird game. The bird has a fixed horizontal position `bird_x` and a vertical position `bird_y`. Smaller `y` means the bird is higher on the screen. The current game state contains: - `bird_y`: current vertical position - `bird_v`: current vertical velocity - `bird_x`: current horizontal position - `screen_height`: height of the screen - `next_pipe`: an object with: - `x`: left edge of the next pipe - `width`: pipe width - `gap_top`: top boundary of the gap - `gap_bottom`: bottom boundary of the gap Game physics: - If the bird jumps on the current frame, its velocity becomes `-8`. - Gravity is `+1` per frame. - Each frame updates in this order: 1. `y += v` 2. `v += 1` - The pipe moves left by `1` unit per frame. - The bird dies if it goes outside the screen bounds or hits a pipe. - A collision with the pipe happens when the bird's `x` lies within the pipe's horizontal range and the bird's `y` is not strictly inside the gap `(gap_top, gap_bottom)`. Return `true` if the bird should jump on the current frame, or `false` otherwise. Your policy should be deterministic and should prioritize survival, especially when the bird is close to the ground or approaching the next pipe at an unsafe height.

Quick Answer: This question evaluates discrete-time physics simulation, real-time decision-making under constraints, collision detection, and deterministic control-policy design that prioritizes survival.

Write a function `solution(state) -> bool` for a simplified Flappy Bird game. The return value should say whether the bird should jump on the current frame. To make the behavior fully testable, use this exact deterministic policy: 1. Simulate the future twice: once with no jump now, and once with a jump now. 2. In the jump simulation, set the bird's velocity to `-8` before the first simulated frame. 3. Each simulated frame updates in this order: `y += v`, then `v += 1`, then the next pipe moves left by `1`. 4. After those updates, the bird dies if: - `y < 0` or `y >= screen_height`, or - the bird is inside the pipe's horizontal range and its `y` is not strictly inside the gap. 5. Treat the pipe's horizontal range as half-open: `[pipe_x, pipe_x + width)`. 6. Stop a simulation as soon as the bird dies or the bird has passed the pipe, meaning `pipe_x + width <= bird_x`. 7. Choose `True` if jumping survives for more simulated frames than not jumping. 8. If both choices survive for the same number of frames, choose the action whose bird gets closer to the center of the gap during any overlapping frame. Use the minimum value of `abs(y - gap_center)` over all frames where the bird horizontally overlaps the pipe. 9. If the two choices are still tied, return `False`. This policy prioritizes survival while still making a deterministic choice when both actions look equally safe.

Constraints

  • All values in `state` are integers.
  • `0 <= bird_y < screen_height` and `1 <= screen_height <= 10^6`.
  • `-10^6 <= bird_v <= 10^6`.
  • `1 <= next_pipe['width'] <= 10^6`.
  • `0 <= next_pipe['gap_top'] < next_pipe['gap_bottom'] <= screen_height`.

Examples

Input: {'bird_y': 18, 'bird_v': 2, 'bird_x': 5, 'screen_height': 20, 'next_pipe': {'x': 15, 'width': 3, 'gap_top': 6, 'gap_bottom': 12}}

Expected Output: True

Explanation: If the bird does not jump, it hits the ground on the first simulated frame. Jumping survives longer, so the policy returns True.

Input: {'bird_y': 4, 'bird_v': -2, 'bird_x': 5, 'screen_height': 20, 'next_pipe': {'x': 7, 'width': 2, 'gap_top': 0, 'gap_bottom': 10}}

Expected Output: False

Explanation: The bird is already high and moving upward. Jumping would immediately go out of bounds above the screen, while waiting safely passes the pipe.

Input: {'bird_y': 15, 'bird_v': 0, 'bird_x': 5, 'screen_height': 20, 'next_pipe': {'x': 6, 'width': 1, 'gap_top': 5, 'gap_bottom': 10}}

Expected Output: True

Explanation: The pipe overlaps on the next frame. Without jumping, the bird is below the gap and collides immediately. Jumping moves it into the gap and past the width-1 pipe.

Input: {'bird_y': 17, 'bird_v': 0, 'bird_x': 5, 'screen_height': 20, 'next_pipe': {'x': 6, 'width': 1, 'gap_top': 5, 'gap_bottom': 18}}

Expected Output: True

Explanation: Both actions survive for the same number of frames, so the tie is broken by distance to the gap center. Jumping places the bird closer to the center of the gap.

Input: {'bird_y': 15, 'bird_v': -1, 'bird_x': 5, 'screen_height': 20, 'next_pipe': {'x': 6, 'width': 1, 'gap_top': 5, 'gap_bottom': 14}}

Expected Output: True

Explanation: Without jumping, the bird reaches `y = 14`, which is exactly `gap_bottom` and therefore a collision because the gap is strict. Jumping avoids that boundary collision.

Input: {'bird_y': 17, 'bird_v': 0, 'bird_x': 5, 'screen_height': 25, 'next_pipe': {'x': 6, 'width': 1, 'gap_top': 4, 'gap_bottom': 22}}

Expected Output: False

Explanation: Both actions survive equally long and are equally close to the gap center during overlap, so the deterministic final tie-break returns False.

Hints

  1. Simulate both possible actions: jump now and do not jump now.
  2. Be careful with the update order and with the fact that the gap is strict: `gap_top < y < gap_bottom`.
Last updated: May 3, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement an In-Memory Database - Coinbase (hard)
  • Implement a Coin-Constrained Jump Strategy - Coinbase (hard)
  • Implement Game Physics and Block Mining - Coinbase (hard)
  • Compute Total Manual Distance - Coinbase (medium)
  • Implement Plus One - Coinbase (medium)