InterviewDB Experience · Los Angeles

Command Parser: Parse and Dispatch Shell-Like Commands from User Input

Interview Experience

Round 1 Coding / OOD

Problem

Design a command parser that reads a command string from the user, parses it into a command name and arguments, and dispatches it to a registered handler. Support flags (e.g., --verbose) and named arguments (e.g., --output=file.txt).

python
class CommandParser:
    def register(self, name: str, handler: callable,
                 description: str = "") -> None:
        ...
    def parse(self, input_str: str) -> dict:

**returns** {"command": str, "args": list, "flags": set, "kwargs": dict}
        ...
    def execute(self, input_str: str) -> any:
        ...
    def help(self) -> str:
        ...

Example

parser = CommandParser()
parser.register("greet", lambda args, **kw: f"Hello, {args[0]}!")
parser.register("add",   lambda args, **kw: sum(int(a) for a in args))

parser.execute("greet Alice --verbose")
# parse -> {"command":"greet","args":["Alice"],"flags":{"verbose"},"kwargs":{}}
# -> "Hello, Alice!"

parser.execute("add 1 2 3 --output=result.txt")
# -> 6, and kwargs has output="result.txt"

parser.execute("unknown")
# -> raises UnknownCommandError

Follow-ups

  1. How do you handle quoted arguments with spaces, like greet "Alice Smith"?
  2. How would you add argument type coercion so a handler can declare it expects integers?
  3. How would you add tab-completion support for registered command names?
  4. How does your design change if you need to support chaining commands with pipes, like list | filter --active?

Full Details

Round 1 Coding / OOD

Problem

Design a command parser that reads a command string from the user, parses it into a command name and arguments, and dispatches it to a registered handler. Support flags (e.g., --verbose) and named arguments (e.g., --output=file.txt).

python
class CommandParser:
    def register(self, name: str, handler: callable,
                 description: str = "") -> None:
        ...
    def parse(self, input_str: str) -> dict:

**returns** {"command": str, "args": list, "flags": set, "kwargs": dict}
        ...
    def execute(self, input_str: str) -> any:
        ...
    def help(self) -> str:
        ...

Example

parser = CommandParser()
parser.register("greet", lambda args, **kw: f"Hello, {args[0]}!")
parser.register("add",   lambda args, **kw: sum(int(a) for a in args))

parser.execute("greet Alice --verbose")
# parse -> {"command":"greet","args":["Alice"],"flags":{"verbose"},"kwargs":{}}
# -> "Hello, Alice!"

parser.execute("add 1 2 3 --output=result.txt")
# -> 6, and kwargs has output="result.txt"

parser.execute("unknown")
# -> raises UnknownCommandError

Follow-ups

  1. How do you handle quoted arguments with spaces, like greet "Alice Smith"?
  2. How would you add argument type coercion so a handler can declare it expects integers?
  3. How would you add tab-completion support for registered command names?
  4. How does your design change if you need to support chaining commands with pipes, like list | filter --active?

About This Question

This is a candidate experience report from a samsara interview during the onsite round.

It covers the following topics: Coding, Ood, Strings, Onsite .