Back to Ramp questions
CodingSoftware Engineer

Flight Activity Tracker

Role: Software Engineer


Given a list of flight records, build a tracker that can answer two questions: which user has flown the most, and where is a specific user located at any given point in time.

Note: Ramp places heavy emphasis on code cleanliness. Interviewers expect well-named variables, clear logic flow, and minimal complexity. Write production-quality code, not just working code.

Problem Statement

You are given flight records in the following format:

python
[
    {
        "user_id": 1,
        "departure_airport": "MIA",
        "departure_time": "2024-10-26T10:00:00",
        "arrival_airport": "JFK",
        "arrival_time": "2024-10-26T14:00:00",
    },
    ...
]

Implement a FlightTracker class:

python
class FlightTracker:
    def __init__(self, records: list[dict]):
        ...

    def most_active_user(self) -> list[int]:
        """Return user_id(s) with the highest flight count."""
        ...

    def location_at(self, user_id: int, timestamp: str) -> str | None:
        """Return the airport where user_id is located at the given ISO timestamp."""
        ...

Part 1 — Most Active User

Return all user_ids tied for the highest number of flights.

python
tracker = FlightTracker(records)
tracker.most_active_user()  # → [1]

Approach: Count flights per user with a Counter. Find the max, return all users that match it.


Part 2 — User Location at a Given Time

Return the airport a user is at for a given timestamp.

Rules:

  • If the timestamp falls after a departure but before or at arrival, the user is en route — return the arrival airport
  • If the timestamp precedes the user's first departure, return the first departure airport
  • If the timestamp follows all their flights, return the last arrival airport
  • If the user has no records, return None
python
tracker.location_at(1, "2024-10-26T11:00:00")  # → "JFK"  (in flight)
tracker.location_at(1, "2024-10-25T08:00:00")  # → "MIA"  (before first flight)
tracker.location_at(1, "2024-11-01T00:00:00")  # → "JFK"  (after all flights)
tracker.location_at(9, "2024-10-26T11:00:00")  # → None   (unknown user)

Approach: Sort each user's flights by departure time at construction. At query time, use binary search (bisect_right) to find the latest flight that has already departed.


Implementation

python
from collections import Counter, defaultdict
from bisect import bisect_right
from datetime import datetime


class FlightTracker:
    def __init__(self, records: list[dict]):
        self._records = [self._parse(r) for r in records]

        self._by_user: dict[int, list[dict]] = defaultdict(list)
        for record in self._records:
            self._by_user[record["user_id"]].append(record)

        for flights in self._by_user.values():
            flights.sort(key=lambda r: r["departure_time"])

    def _parse(self, record: dict) -> dict:
        return {
            **record,
            "departure_time": datetime.fromisoformat(record["departure_time"]),
            "arrival_time": datetime.fromisoformat(record["arrival_time"]),
        }

    def most_active_user(self) -> list[int]:
        flight_counts = Counter(r["user_id"] for r in self._records)
        if not flight_counts:
            return []
        peak = max(flight_counts.values())
        return [uid for uid, count in flight_counts.items() if count == peak]

    def location_at(self, user_id: int, timestamp: str) -> str | None:
        flights = self._by_user.get(user_id)
        if not flights:
            return None

        ts = datetime.fromisoformat(timestamp)
        departures = [f["departure_time"] for f in flights]

        idx = bisect_right(departures, ts) - 1

        if idx < 0:
            return flights[0]["departure_airport"]

        current = flights[idx]
        if ts > current["departure_time"]:
            return current["arrival_airport"]
        return current["departure_airport"]

Complexity

OperationTimeSpace
ConstructionO(n log n) per userO(n)
most_active_userO(n)O(u) users
location_atO(log n)O(1)

Edge Cases

  • Exact departure timestamp: User hasn't left yet → return departure airport
  • Exact arrival timestamp: ts > departure_time is true → return arrival airport
  • No flights for user: Return None
  • Single flight: Works correctly with both before/during/after cases

Follow-ups

  • How would you handle flights with identical departure times for the same user?
  • How would you extend location_at to return a full itinerary between two timestamps?
  • How would you make the tracker updatable — i.e., new flights arrive as a stream?
  • How would you modify most_active_user to accept an optional date range filter?

Reported Solution (independent write-up)


Variant (independent report — verbatim prompt and dataset)

Two independent community reports (a screenshot of the prompt and a shared message.txt with "code for flight qn") captured the original wording, dataset, and test expectations as delivered:

Some of our customers take a lot of flights. We'd like to create a feature that helps admins better understand their employees' travel. You're given a set of flights from one customer in JSON form. The flights look like the following:

python
FLIGHT_DATA = [
    {
        "departure_airport": "JFK",
        "departure_time": "2021-11-15T22:49:00Z",  # ISO-8601 string
        "arrival_airport": "BOS",
        "arrival_time": "2021-11-16T00:12:00Z",
        "user_id": 2
    },
    {
        "departure_airport": "JFK",
        "departure_time": "2021-11-04T03:47:00Z",
        "arrival_airport": "SEA",
        "arrival_time": "2021-11-04T09:12:00Z",
        "user_id": 3
    },
    {
        "departure_airport": "MIA",
        "departure_time": "2021-11-15T15:10:00Z",
        "arrival_airport": "MSY",
        "arrival_time": "2021-11-15T18:21:00Z",
        "user_id": 1
    },
    {
        "departure_airport": "SEA",
        "departure_time": "2021-11-04T03:47:00Z",
        "arrival_airport": "JFK",
        "arrival_time": "2021-11-04T09:12:00Z",
        "user_id": 2
    },
    {
        "departure_airport": "SEA",
        "departure_time": "2021-11-15T22:49:00Z",
        "arrival_airport": "SFO",
        "arrival_time": "2021-11-16T00:12:00Z",
        "user_id": 3
    },
    {
        "departure_airport": "BOS",
        "departure_time": "2021-10-28T11:47:00Z",
        "arrival_airport": "JFK",
        "arrival_time": "2021-10-28T14:02:00Z",
        "user_id": 3
    },
    {
        "departure_airport": "JFK",
        "departure_time": "2021-10-29T18:31:00Z",
        "arrival_airport": "MIA",
        "arrival_time": "2021-10-29T21:36:00Z",
        "user_id": 1
    },
    {
        "departure_airport": "MSY",
        "departure_time": "2021-11-21T18:00:00Z",
        "arrival_airport": "IAH",
        "arrival_time": "2021-11-21T19:41:00Z",
        "user_id": 1
    },
    {
        "departure_airport": "SFO",
        "departure_time": "2021-10-26T16:15:00Z",
        "arrival_airport": "JFK",
        "arrival_time": "2021-10-26T21:34:00Z",
        "user_id": 1
    },
    {
        "departure_airport": "SFO",
        "departure_time": "2021-10-28T11:47:00Z",
        "arrival_airport": "SEA",
        "arrival_time": "2021-10-28T14:02:00Z",
        "user_id": 2
    }
]

Part 1. Write a function to find which user is taking the most flights.

Part 2. Write a function that takes in a user_id and a time and returns the location of the user at the given time.

python
def get_user_location_at_time(user_id: int, time: str) -> str:

Example usage:

text
In [1]: get_user_location_at_time(user_id=1, time="2021-10-28T14:30:00Z")
Out[1]: "JFK"

In [2]: get_user_location_at_time(user_id=2, time="2021-10-28T14:30:00Z")
Out[2]: "SEA"

Expected behavior from the shared test cases (user_id, query time → expected location):

python
testing_data = [
    (1, "2021-10-25T00:00:00Z"),  # SFO
    (1, "2021-10-26T18:00:00Z"),  # JFK
    (1, "2021-10-26T22:00:00Z"),  # JFK
    (1, "2021-10-29T18:00:00Z"),  # JFK
    (1, "2021-10-29T18:40:00Z"),  # MIA
    (1, "2021-11-15T15:10:00Z"),  # MIA
    (1, "2021-11-15T15:10:01Z"),  # MSY
    (1, "2021-11-21T18:00:00Z"),  # MSY
    (1, "2021-11-21T18:00:01Z"),  # IAH
    (1, "2021-12-01T00:00:00Z"),  # IAH
    (2, "2021-11-01T00:00:00Z"),  # SEA
    (3, "2021-11-01T00:00:00Z"),  # JFK
    (4, "2021-11-01T00:00:00Z"),  # None (unknown user)
]

Note the expected semantics in the test cases: at the exact departure timestamp the user is still at the departure airport; one second after departure they count as at the arrival airport. The solution shared alongside this report is the same FlightIndex draft preserved in solutions/flight-index-and-user-location-draft.md (built on three mappings: per-user flight counts, per-user flights sorted by departure time, and per-user sorted departure-time lists for binary search).

Source: community reports, March–April 2026