Implement a Flappy Bird Jump Agent
CoinbaseSoftware Engineer · Technical ScreenLast updated
This problem tests your ability to design a simple heuristic-based agent that navigates a 2D environment by calculating clearance intervals and applying physics-based jump logic.
Problem Statement
You are building an autopilot agent for a 1D vertical game similar to Flappy Bird. The game world is a 2D grid where gravity pulls the bird down by gravity units each tick, and a jump action provides an upward velocity of jump_strength.
Your bird is at position (0, y) and moving with vertical velocity v. A series of obstacles (pipes) are approaching from the right at a constant horizontal speed. Each obstacle is defined by its horizontal distance x from the bird and the vertical gap interval [bottom_y, top_y].
Implement the function should_jump(y, v, gravity, obstacles) -> bool.
y: Current vertical position (float).v: Current vertical velocity (float).gravity: Constant downward acceleration (float).obstacles: A list of tuples(x, bottom_y, top_y), wherexis the horizontal distance to the pipe,bottom_yis the lower bound of the gap, andtop_yis the upper bound.
Constraints
- The bird must stay within
[0, ceiling_height]. - The agent must decide whether to trigger a jump at the current tick.
- The next tick position
y_newwill bey + vand the velocity will bev - gravityif no jump occurs, orv + jump_strengthif a jump occurs.
Example
# gravity=1, jump_strength=5, bird_y=10, v=0
# obstacles=[(5, 8, 12)]
# should_jump returns True if predicted trajectory will hit the lower pipe edge.
Edge Cases
- Bird is already at the peak of the jump.
- Pipe is very close to the bird (
xis small). - Bird is already positioned safely within the gap.
Hints
Hint 1
Predict the bird's vertical position over the next several ticks using simple kinematic equations: y_next = y + v - 0.5 * gravity.
Hint 2
Focus only on the nearest obstacle that the bird will intersect horizontally within the next few ticks.
Hint 3
Calculate if the bird's predicted y-coordinate will fall outside the [bottom_y, top_y] range at the specific tick when the bird's x-coordinate reaches the obstacle's x-coordinate.
Follow-up questions
- How would you modify your agent if the 'jump' action had a cooldown period?
- How would you implement a 'look-ahead' feature to account for a sequence of two pipes that are close together?
- If the environment were stochastic (e.g., gravity changes slightly every tick), how would your deterministic policy need to change?
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.