KatInterview

Receding-Horizon Controller for a Sinking Drone

System DesignModel answer
MediumModel answerSystem Designsimulationsearchgame-ai

Last updated

Build a tick-by-tick autopilot that uses short deterministic lookahead to steer a sinking drone through a series of vertical gap obstacles.

Receding-Horizon Controller for a Sinking Drone

Background

You are building an autopilot for a small underwater drone in a side-scrolling obstacle course. The drone moves forward at a constant speed and continuously loses altitude due to buoyancy drift. Once per game tick your controller can fire a thruster, which instantly gives the drone a fixed upward kick; if the thruster is not fired the drone keeps sinking a little faster each tick. A series of wall pairs block the path — each pair has a narrow vertical gap the drone must pass through. Clipping any wall, the sea floor, or the water surface ends the run immediately.

Your task is to implement should_thrust, a function called once per tick that returns True to fire the thruster or False to let the drone drift.

Interface

from dataclasses import dataclass


@dataclass
class WallPair:
    x_left: float      # left edge of the wall pair
    x_right: float     # right edge of the wall pair
    gap_lo: float      # lower boundary of the safe gap (top of the bottom wall)
    gap_hi: float      # upper boundary of the safe gap (bottom of the top wall)


@dataclass
class DroneState:
    x: float           # horizontal position (drone center)
    y: float           # vertical position above the floor (drone center); increases upward
    vy: float          # current vertical velocity; positive means moving upward
    walls: list        # visible WallPairs, sorted by x_left


def should_thrust(state: DroneState) -> bool:
    """Called once per tick. Return True to fire the upward thruster."""

What is expected

Implement should_thrust so the drone clears a sequence of wall pairs on maps it has not been tuned to. The gap heights and positions vary between maps, so a solution hard-coded to one map's timing does not qualify.

Describe your overall approach, justify your choice of lookahead depth, explain how you handle the case where no action can save the drone, and analyse the time complexity of your solution.

Physics constants (assumed — confirm with your interviewer)

ConstantValueMeaning
DRIFT1.0vy decreases by this each tick when not thrusting
THRUST_VY3.0vy is set to this when thrusting
SPEED1.0horizontal advance per tick (constant)
DRONE_R0.5collision radius of the drone
FLOOR0.0y-coordinate of the floor
CEILING30.0y-coordinate of the water surface

Clarifying questions to raise before coding

  • Can the controller see all upcoming wall pairs or only the next one?
  • Is there a per-tick compute budget the solution must respect?
  • Should the function degrade gracefully on an already-unwinnable state, or can it assume the given state is always recoverable?

Hints

Hint 1

Because the physics are fully deterministic and the constants are known, you can simulate the future exactly — no probability or estimation needed. Think about what structure that gives you to exploit.

Hint 2

The key question is not just 'does this action survive the next tick?' but 'does this action leave me in a state where some sequence of future actions can still survive?' A short recursive search over the two choices per tick can answer that.

Hint 3

Prefer not thrusting when both options are safe — it conserves energy and avoids over-correction. Re-run the search from scratch every tick rather than committing to a plan; that way the controller adapts as walls come into view. Consider: how many ticks ahead do you need to look to react in time?