Debug Feature Updates in a Laptop Management CLI
ZiprecruiterMachine Learning Engineer · Technical ScreenLast updated
Implement and debug a stateful command-line interface processor that tracks device feature configurations, updates targeted laptops without state leakage across devices, and rolls back on validation failures.
Problem Statement
An internal IT provisioning service manages software feature flags and installed configurations across company laptops. You are tasked with implementing the core execution engine for a CLI tool that parses and applies batch feature updates to a registry of laptops.
Each laptop in the inventory has:
id(str): Unique device identifier.model(str): Hardware model designation.features(setofstr): Set of currently enabled feature flags.
The update process accepts an initial list of laptop records and a sequence of text commands executed sequentially:
SELECT: Sets the target device for subsequent commands. If `` does not exist in inventory, the command fails and processing stops.ENABLE: Stages a feature to be added to the currently selected device. If no device is selected, processing stops.DISABLE: Stages a feature to be removed from the currently selected device. If no device is selected, processing stops.COMMIT: Applies all staged changes for the selected device. If a feature staged forENABLEis already present, or staged forDISABLEis not present, validation fails.RESET: Clears any uncommitted staged changes for the currently selected device.
If any command encounters an error or validation failure, the function must immediately abort, discard all staged and committed changes from the entire batch, and return (False, original_devices).
If all commands complete successfully, return (True, updated_devices) where updated_devices preserves the original order of laptops.
Function Signature
def process_laptop_updates(
devices: list[dict],
commands: list[str]
) -> tuple[bool, list[dict]]:
"""
Applies CLI commands to a list of device records.
Each device dict has keys: 'id' (str), 'model' (str), 'features' (set[str]).
Returns (success, final_devices).
"""
Constraints
1 <= len(devices) <= 5001 <= len(commands) <= 2,000- Device IDs, models, and feature names are alphanumeric strings (
1 <= len <= 64). - All device IDs in
devicesare distinct.
Examples
Example 1
devices = [
{"id": "lap-01", "model": "X1", "features": {"docker", "vpn"}},
{"id": "lap-02", "model": "M2", "features": {"vpn"}}
]
commands = [
"SELECT lap-02",
"ENABLE docker",
"COMMIT"
]
# Returns: (True, [
# {"id": "lap-01", "model": "X1", "features": {"docker", "vpn"}},
# {"id": "lap-02", "model": "M2", "features": {"docker", "vpn"}}
# ])
Example 2
devices = [
{"id": "lap-01", "model": "X1", "features": {"docker"}}
]
commands = [
"SELECT lap-01",
"ENABLE docker",
"COMMIT"
]
# Returns: (False, [{"id": "lap-01", "model": "X1", "features": {"docker"}}])
# Explanation: 'docker' was already enabled on 'lap-01', causing validation to fail on COMMIT.
Hints
Hint 1
Work on deep copies of the original device dictionaries so that a failed command sequence leaves the original input unmodified.
Hint 2
Maintain staged changes (e.g., features to add and features to remove) separately from committed device features until the COMMIT command is validated.
Hint 3
Ensure that switching between devices with SELECT does not automatically discard or accidentally leak uncommitted staged changes from a previous device.
Follow-up questions
- How would you support dependent features where enabling feature A requires feature B to also be enabled?
- If updates were streamed concurrently from multiple operators, how would you implement optimistic concurrency control?
- How would you refactor this processor to return structured, user-friendly error messages indicating the exact command and line number that failed?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.