Katkat

Design a bus simulation metric

InstacartSoftware Engineer · Onsite

Last updated

This problem tests your ability to design a telemetry metric for a simulation environment, requiring you to define efficiency, implement instrumentation within existing logic, and explain how to validate the metric's accuracy.

Scenario

You are working on a bus transit simulation codebase. The simulation models a fleet of buses traversing a fixed route and stopping at various stations to pick up passengers. You have been tasked with identifying why the current fleet configuration is 'inefficient' and providing a quantitative measure to track this.

The Task

  1. Define the Metric: Propose a single numerical metric that represents the 'efficiency' of the bus service. Consider factors such as passenger wait times, bus capacity utilization, and fuel consumption.
  2. Instrumentation: You are provided with a Bus class and a Station class. Below is the simplified interface:
class Bus:
    def __init__(self, id, capacity):
        self.capacity = capacity
        self.passengers = []
        # ... methods: move(), pick_up(), drop_off()

class Station:
    def __init__(self, id):
        self.queue = [] # list of passengers
        # ... methods: add_passenger()

Explain where in the code you would inject telemetry hooks to collect data for your metric without significantly impacting the simulation's performance. 3. Validation: Describe how you would verify that your metric is actually capturing 'efficiency' rather than noise. What baseline or test case would you run to prove your metric reacts as expected to changes (e.g., doubling the fleet size)?

Constraints

  • Do not modify the core movement logic of the simulation.
  • Your metric must be calculable from the logs generated during a simulation run.
  • The simulation can involve up to 1,000 buses and 10,000 passengers per run.

Hints

Hint 1

Consider balancing passenger-centric metrics (like average wait time) against operator-centric metrics (like empty seat miles).

Hint 2

Think about using a decorator pattern or an observer pattern to hook into the 'pick_up' and 'drop_off' events without tightly coupling the collection logic to the business logic.

Hint 3

For validation, suggest running the simulation with a 'perfect' static schedule versus a 'random' schedule and comparing the divergence in your metric.

Follow-up questions

  • How would you handle the storage and aggregation of these metrics if the simulation is distributed across multiple nodes?
  • If your metric shows a high variance, how do you determine if it is due to a simulation bug or a statistical anomaly?
  • How would you modify your design if the requirement shifted from 'average efficiency' to 'worst-case latency'?