KatInterview

Designing and Building an In-Memory RESTful Q&A Service

System DesignModel answer
MediumModel answerSystem Designapi-designsystem-designpython

Last updated

Architect and implement a minimal Q&A web service with HTTP endpoints for question creation, retrieval, and search, including pagination and data validation.

You are tasked with building a lightweight REST service implementing the core capabilities of a Stack Overflow-style Question and Answer platform.

Requirements

  1. API Endpoints:

    • Create Question: Accepts a title and detailed content body, with optional fields for user-defined tags and author identifier.
    • Search/Query Questions: Allows searching by text contained within the title or body, and optionally filtering by tags.
    • Get Question by ID: Fetches the complete metadata of a single question using its unique integer identifier.
  2. Design Expectations:

    • Define concrete choices for data persistence (e.g., an in-memory repository abstracted behind a repository interface).
    • Implement pagination and ordering rules for search queries.
    • Include validation for edge cases (e.g., empty fields, invalid query parameters, missing resources).
    • Ensure safety under concurrent read and write operations.

Provide an architectural breakdown of the contract, a Discussion of trade-offs, and a complete code implementation.

Hints

Hint 1

Abstract your storage mechanism behind a repository interface or class so that an in-memory dictionary can easily be swapped with a relational database later.

Hint 2

Consider how tags are stored and matched. Standardizing tags to lowercase and stripping whitespace upon ingestion simplifies filtering logic.

Hint 3

Ensure read/write operations against shared memory structures are guarded with locks or thread-safe primitives to handle concurrent API requests safely.