Design a simulation scenario runner with moving actors and boundary clamping
Company: Waymo
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design and implement a `SimulationRunner` class that loads and runs simulation scenarios for an **autonomous vehicle stack**. The runner manages **actors** (vehicles, pedestrians, obstacles) inside a bounded **environment** and steps the simulation forward at discrete time intervals.
The constructor `SimulationRunner(width, height)` sets the environment, a rectangle from `(0, 0)` to `(width, height)`. Each actor has a unique string ID, a type (`"vehicle"`, `"pedestrian"` or `"obstacle"`), a position `(x, y)` and a velocity `(vx, vy)`.
At each simulation step, every non-obstacle actor's position is updated by adding its velocity components. Positions are then **clamped** to the environment boundaries. Obstacles are stationary and never move, whatever velocity they were given.
Implement these methods:
- `addActor(actor_id: str, actor_type: str, x: float, y: float, vx: float, vy: float) -> bool`: add an actor with the given ID, type, initial position and velocity. Returns `True` if it was added and `False` if the ID already exists. Obstacles always have zero velocity. The initial position is clamped to the environment boundaries.
- `removeActor(actor_id: str) -> bool`: remove the actor with the given ID. Returns `True` if it was removed and `False` if it was not found.
- `step() -> int`: advance the simulation by one time step, moving every actor by its velocity and clamping to the boundaries. Obstacles do not move. Returns the current time after the step.
- `getActorPosition(actor_id: str) -> List[float]`: returns the actor's position as `[x, y]`, or `[-1.0, -1.0]` if the actor is not found.
- `getActorCount(actor_type: str) -> int`: returns the number of actors of the given type. Passing `"all"` returns the total number of actors.
- `setActorVelocity(actor_id: str, vx: float, vy: float) -> bool`: update the velocity of an existing actor. Returns `True` on success and `False` if the actor is not found or is an obstacle.
- `getTime() -> int`: returns the current simulation time (the number of steps executed).
- `reset() -> None`: removes all actors and sets the time back to 0.
Example in a 10 by 10 environment:
```text
SimulationRunner(10, 10)
addActor("car1", "vehicle", 2, 3, 4, 0) -> True
addActor("ped1", "pedestrian", 5, 5, -1, 2) -> True
addActor("rock", "obstacle", 12, -3, 1, 1) -> True stored at (10, 0) with velocity (0, 0)
addActor("car1", "vehicle", 0, 0, 1, 1) -> False ID already exists
step() -> 1 car1 (6, 3), ped1 (4, 7)
step() -> 2 car1 (10, 3), ped1 (3, 9)
step() -> 3 car1 (10, 3), ped1 (2, 10)
getActorPosition("car1") -> [10.0, 3.0]
getActorPosition("rock") -> [10.0, 0.0]
setActorVelocity("rock", 1, 1) -> False
setActorVelocity("car1", -3, 1) -> True
step() -> 4 car1 (7, 4), ped1 (1, 10)
getActorCount("vehicle") -> 1
getActorCount("all") -> 3
removeActor("ped1") -> True
getActorPosition("ped1") -> [-1.0, -1.0]
getTime() -> 4
reset()
getTime() -> 0
getActorCount("all") -> 0
```
On the third step car1 would reach x = 14 and is held at 10, and ped1 would reach y = 11 and is held at 10. Clamping changes only the position, so ped1 keeps its velocity (-1, 2) and stays on the top edge during step 4 while still moving left.
```hint State layout
Every method except step and reset reads or writes one actor by its ID. Choose a store that makes those calls constant time, then ask what else you could keep up to date so that getActorCount does not have to scan.
```
```hint Where obstacles could move
A velocity can reach an actor through more than one method. Check each of those paths for obstacles.
```
### Constraints & Assumptions
- `1 <= width, height <= 10000`
- `-10000 <= x, y, vx, vy <= 10000`
- Positions and velocities are floats.
- Actor IDs are unique strings.
- Actor types are one of `"vehicle"`, `"pedestrian"`, `"obstacle"`.
- At most `1000` actors exist at any time.
- At most `10000` step calls are made per scenario.
### Clarifying Questions to Ask
- Do actors interact with each other (collisions, blocking), or only with the boundary?
- What should `getActorCount` return for a type string that is not one of the three types?
- Is the runner called from a single thread, or can something read positions while `step` is running?
- Does `step` need to process actors in a fixed order, for example so that logs are reproducible?
### What a Strong Answer Covers
- A state layout keyed by actor ID that gives constant-time add, remove, lookup and velocity update, with the cost of `step` and `getActorCount` stated.
- Clamping applied both when an actor is added and after every move, on each axis independently and against both the lower and upper bound.
- Obstacles kept stationary on every path: zero velocity forced on insert, refused in `setActorVelocity`, and not moved in `step`.
- Exact return values on the edge paths: duplicate ID, missing ID, the `[-1.0, -1.0]` sentinel, `"all"`, and the time returned by `step`.
- A `reset` that clears every piece of state, including any derived counters.
### Follow-up Questions
- How would you add `step(k)` that advances `k` steps without looping `k` times over every actor?
- How would you detect collisions between actors during a step without comparing every pair?
- How would you load scenarios from a file and make runs reproducible, so that a failing scenario can be replayed exactly?
- If a visualizer reads positions on another thread while `step` runs, how do you keep it from seeing a half-updated world?
Overview: Implement a class that runs a simple autonomous vehicle simulation: add and remove vehicles, pedestrians and obstacles, step time forward, and clamp every position to a rectangular boundary. It tests object-oriented design, hash map state management, edge-case handling and complexity reasoning.