KatInterview

Wind Field Tracker for Hot Air Balloons

Data Structure DesignModel answer
MediumModel answerData Structure Designevent-simulationspatial-aggregationtime-based-state

Last updated

Build a system that tracks balloon altitudes, computes spatially-varying wind exposure from multiple sources, and identifies stable balloons above a threshold.

You're building a real-time tracking system for a hot air balloon competition. Your task is to implement a BalloonFestival class that manages balloon positions, wind conditions at different altitudes, and determines which of your team's balloons are performing well.

API Specification

Constructor

__init__(yourBalloonNames: list[str])

Initialize the system with the names of your team's balloons. Any balloon names encountered later that aren't in this list belong to competing teams. Each balloon name is unique across all teams. Constraint: 1 ≤ len(yourBalloonNames) < 2^20.

Methods

BalloonAscended(timestamp: float, balloonName: str, altitude: float) -> bool Records that a balloon has launched or moved to a new altitude at the given timestamp. Balloons start in a stable state when they ascend. Returns True if successful, False otherwise.

BalloonDescended(timestamp: float, balloonName: str) -> bool Records that a balloon has landed and is no longer airborne. Returns True if successful, False otherwise.

SetWindSpeed(timestamp: float, altitude: float, windSpeed: float) -> bool Updates the wind speed centered at a specific altitude. Constraints: 0 < altitude < 2^15 and 0 ≤ windSpeed < 2^5. Returns True if successful, False otherwise.

InspectBalloons(timestamp: float) -> list[str] Returns a sorted list of your team's balloon names that meet both criteria at the given timestamp:

  1. The balloon is stable
  2. The balloon's altitude is at or above the highest stable competitor balloon

If no balloons qualify, return an empty list.

Wind Physics Model

Wind sources at different altitudes contribute to the total wind experienced by a balloon. For each defined wind source:

contribution = windSpeed / (1 + ((balloonAltitude - sourceAltitude) / 100)²)

The total wind speed at a balloon's altitude is the sum of all source contributions.

Stability Rules

  • A balloon becomes unstable immediately when the wind speed at its altitude exceeds 15 m/s
  • An unstable balloon regains stability only after experiencing wind ≤ 15 m/s continuously for at least 300 seconds
  • When a balloon moves to a new altitude, it resets to stable state and then immediately checks wind conditions at the new location

Additional Requirements

  • Timestamps must be non-decreasing; operations with earlier timestamps than previously seen should fail
  • Design efficient data structures to handle many events over time
  • Handle edge cases like balloons moving while unstable, overlapping wind sources, and boundary conditions

Hints

Hint 1

Wind conditions are piecewise constant between events—nothing changes until someone moves a balloon or adjusts a wind source

Hint 2

Track per-balloon state lazily: record when calm conditions began rather than scheduling future recovery events

Hint 3

When a wind source changes, you can update all affected balloons incrementally rather than recomputing from scratch