Interview conceptSoftware Engineering Fundamentals

API Design, Modularization, And Error Handling

Asked of: Software Engineer

Last updated

What's being tested

These problems test API design: turning mutable state and operations into a small, predictable interface with clear invariants, error semantics, and extension points. They also probe modularization—separating parsing, domain logic, storage, and presentation so each component can be tested independently. Jane Street cares because production code is composed, reviewed, reused, and depended on by other engineers; a locally clever implementation is not enough if its contract is ambiguous or its failures are difficult to diagnose.

Core knowledge

  • Define the contract before implementation: specify inputs, outputs, state transitions, ownership, mutation, ordering, complexity, and failure behavior. A useful API makes invalid states difficult to represent rather than relying on every caller to remember hidden rules.

  • Separate layers by responsibility: keep parsing and validation at the boundary, domain logic in a pure or mostly pure core, and I/O or framework adapters outside it. This makes an interpreter, game engine, or reusable library testable without sockets, files, or global state.

  • Design around invariants: for a board, every occupied cell must contain one valid player and gravity must hold; for a stack machine, operands must be consumed in the specified order. Check invariants after mutations during development, then decide which checks remain in production.

  • Choose representations that expose required operations: a Connect Four column-height array gives O(1) placement and a flat board gives predictable indexing; a stack machine needs explicit operand order. Avoid abstractions whose convenience hides a costly scan or invalid state.

  • Make errors part of the API: distinguish programmer misuse, invalid user input, unavailable resources, and internal bugs. Use typed exceptions or result values consistently; include operation, relevant identifiers, and recovery guidance without leaking secrets or implementation details.

  • Prefer explicit state transitions and ownership: methods such as play, undo, and redo should document whether they mutate in place, return a new value, or invalidate prior handles. Define whether snapshots are deep copies, persistent structures, or views.

  • Use composition over entangled conditionals: isolate Parser, Instruction, Machine, and Environment rather than making one function interpret text, mutate storage, and format errors. Interfaces should represent stable capabilities, not every implementation detail.

  • Treat undo/redo as a history model: a standard design keeps a past stack and future stack; a new successful mutation clears future history, while undo moves one state from past to future. Failed operations must not alter either stack.

  • Specify compatibility and evolution: preserve behavior for existing callers, add optional fields with safe defaults, and avoid changing exception types or positional meanings casually. For public APIs, use semantic versioning, deprecation periods, and contract tests across versions.

  • Test behavior at multiple boundaries: unit-test pure transition functions, property-test invariants, and integration-test the public API. Cover empty state, full capacity, malformed input, repeated undo/redo, integer overflow, unknown identifiers, and failure atomicity.

  • Document complexity and thread assumptions: state whether operations are O(1), O(rows × columns), or proportional to history size, and whether concurrent calls are safe. If synchronization is absent, say so rather than implying that a library is thread-safe.

Worked example — Implement Connect Four with bottom push-up

For “Implement Connect Four with bottom push-up,” a strong candidate first clarifies board dimensions, player representation, whether a move means dropping or pushing from the bottom, win directions, draw behavior, and the exact undo/redo contract. They would state the invariants and organize the solution around board representation, legal move application, win detection, and history management. A compact representation might store each column as a sequence and maintain heights, while a rectangular grid makes directional win checks easier to explain and extend. The public API could expose play(column, player), winner(), undo(), redo(), and a read-only board snapshot, while keeping mutation private to the game object. Win detection should check horizontal, vertical, and both diagonal directions from the newly changed cell rather than rescanning every line after every move. The explicit tradeoff is between a simple full-board scan, roughly O(RC)O(RC) per move, and localized checks, roughly O(R+C)O(R+C) in the worst direction but typically constant for fixed win length. They would also explain that an invalid move must leave the board and history unchanged. If they had more time, they would add property-based tests for gravity, reversible moves, and sequences where a new move invalidates the redo branch.

A second angle

For “Turning Interview Code into a Reusable Library or API,” the same discipline shifts from game-state correctness to consumer-facing stability. The candidate should identify the minimal public surface, move implementation details behind private modules, and provide typed inputs, outputs, documentation, and examples. They should discuss packaging, dependency boundaries, semantic versioning, and whether errors are exceptions or explicit result objects. A key difference is that compatibility with unknown callers matters more than optimizing one known execution path.

Common pitfalls

Pitfall: Treating error handling as a collection of ad hoc try/catch blocks is tempting, but it produces inconsistent contracts and partial mutations. Instead, classify failures, define atomicity, and ensure every public operation either completes its documented transition or leaves state unchanged.

Pitfall: Jumping directly into classes and code without clarifying semantics makes a reasonable design look accidental. State assumptions first, name the invariants, and explain why each public method exists; interviewers can then evaluate the design rather than infer its intent.

Pitfall: Saying “use interfaces and unit tests” without identifying boundaries is too shallow. Name the modules, their dependencies, representative edge cases, complexity, versioning behavior, and one concrete tradeoff such as snapshots versus command-based history.

Connections

An interviewer may pivot to design by contract, property-based testing, immutability and persistent data structures, or semantic versioning. They may also ask how the same API behaves under concurrency, serialization, or a network boundary where retries and idempotency become relevant.

Further reading

  • RFC 9457: Problem Details for HTTP APIs — a concrete standard for structured, machine-readable API errors.

  • Designing Data-Intensive Applications, Martin Kleppmann — strong treatment of abstractions, failure modes, consistency, and distributed API tradeoffs.

  • API Design Patterns, JJ Geewax — practical guidance on resource modeling, compatibility, pagination, errors, and evolving public interfaces.

Practice questions

Related concepts