Design an input remapping system that binds device keys to game actions
Company: Unity
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a system that abstracts player input from multiple device types (Keyboard, Controller, Touch) into named actions. The system should allow registering actions with default input bindings and support remapping these bindings at runtime.
Each binding associates a `(device_type, input_key)` pair with an action name. A given device-key pair can only be bound to one action at a time. When an input is triggered, the system looks up the corresponding action and records it as the last triggered action for that device type.
The system should also support querying all bindings for a given action and unbinding actions from specific devices.
Implement a class `InputRemappingSystem` with the following methods:
- `register_action(action_name: str, device_type: str, input_key: str) -> None`: Register an action with a default input binding for a specific device type.
- `remap_action(action_name: str, device_type: str, new_input_key: str) -> bool`: Remap an existing action to a new input key on the given device. Returns `True` if successful, `False` if no existing binding was found for that action on the device.
- `trigger_input(device_type: str, input_key: str) -> str`: Trigger an input from a device type with a specific key. Returns the bound action name, or an empty string if the input is not bound.
- `get_last_triggered_action(device_type: str) -> str`: Return the last successfully triggered action for the given device type, or an empty string if no action has been triggered on that device.
- `get_bindings_for_action(action_name: str) -> List[str]`: Return a sorted list of all bindings for the given action, formatted as `"device_type:input_key"`.
- `unbind_action(action_name: str, device_type: str) -> bool`: Remove the binding for an action on a specific device type. Returns `True` if a binding was removed, `False` otherwise.
For example, on a new system this sequence of calls returns the values shown:
```text
register_action("Jump", "Keyboard", "Space")
register_action("Jump", "Controller", "A")
trigger_input("Keyboard", "Space") -> "Jump"
trigger_input("Touch", "Tap") -> ""
get_last_triggered_action("Keyboard") -> "Jump"
get_last_triggered_action("Controller") -> ""
remap_action("Jump", "Keyboard", "W") -> True
trigger_input("Keyboard", "Space") -> ""
get_bindings_for_action("Jump") -> ["Controller:A", "Keyboard:W"]
unbind_action("Jump", "Controller") -> True
remap_action("Jump", "Controller", "B") -> False
get_bindings_for_action("Jump") -> ["Keyboard:W"]
```
Space stops firing once Jump moves to W. The last remap fails because Jump no longer has a Controller binding.
```hint Two directions of lookup
`trigger_input` searches by `(device_type, input_key)`, while `remap_action` and `unbind_action` search by `(action_name, device_type)`. Pick structures that make both searches cheap, then list what has to change in each of them on every write.
```
```hint Shared keys
Before writing a binding, check who already owns that device-key pair. Work out what the previous owner's records should look like afterward.
```
### Constraints & Assumptions
- Action names and input keys are non-empty strings.
- Device types are one of `"Keyboard"`, `"Controller"`, `"Touch"`.
- At most 1000 operations will be performed.
- A `(device_type, input_key)` pair can be bound to only one action at a time.
### Clarifying Questions to Ask
- Can one action hold two keys on the same device, such as W and Up Arrow for a move action? If so, which of them does `remap_action` change?
- If `register_action` or `remap_action` targets a device-key pair that another action already owns, should the new binding take it over or should the call fail?
- Should `get_last_triggered_action` keep returning an action after that action has been unbound or remapped?
- Is the system called only from the game loop thread, or can input events arrive from other threads?
### What a Strong Answer Covers
- The data structures behind each lookup direction, and how they stay consistent after every register, remap and unbind.
- What happens to the previous owner when a device-key pair is reassigned, in both `register_action` and `remap_action`.
- Correct results for the missing cases: unbound keys, unknown actions, devices with no binding for the action, and devices that never triggered anything.
- Sorted, deterministic output from `get_bindings_for_action` and the cost of each operation.
- Stated assumptions for the ambiguous rules, with a reason for each choice.
### Follow-up Questions
- How would you support a primary and a secondary key per device, and how would the signature of `remap_action` change?
- How would you persist a player's custom bindings and offer a "reset to defaults" option per action?
- How would you handle separate action maps for gameplay and menus, where the same key means different things in each?
- Players often expect the displaced action to receive the key that was just freed. How would you implement that swap, and what should the API return so a settings screen can show what changed?
Overview: Design a class that maps keyboard, controller and touch inputs to named game actions and lets players remap those bindings at runtime. It tests data structure choice for lookups in both directions, keeping state consistent when a key changes owner, and handling missing or conflicting bindings.