← Two Sigma Interview InsightsSoftware Engineer

Two Sigma·Software Engineer·Onsite - System Design / Architecture·Senior

SeniorPrefer not to say
Jun 2026

Summary

Two Sigma system design round, focused entirely on a vending machine OOP problem. Way more depth required than I expected, the interviewer cared a lot about both the domain model and the test coverage, not just the high-level diagram.

Questions Asked (2)

Q1

Design a vending machine using object-oriented principles. Walk through the core domain model, the user-facing API, an explicit state machine, and relevant design patterns.

System DesignTechnical Trade-offsData Modeling
Author's notes

I started with the obvious classes (Item, Slot, VendingMachine) and felt pretty good until they pushed on the state machine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and scope, then define the core domain model (Inventory, Product, Coin/Bill, Transaction) and user-facing API. Next, design an explicit state machine for the vending machine's behavior, and finally discuss design patterns (State, Strategy, Factory) that make the design extensible and maintainable.

Pro tip: Emphasize separation of concerns: keep the domain model pure and use the State pattern to encapsulate state-specific behavior, which makes adding new states or payment methods easy without modifying existing code.

1. Clarify Requirements and Scope

Ask questions to understand constraints: what payment methods (coins, bills, cards), product types, inventory tracking, refunds, and whether it's a single machine or networked. Define functional and non-functional requirements.

2. Define Core Domain Model

Identify key entities: Product (name, price, code), Inventory (stock per product), Coin/Bill denominations, Transaction (amount inserted, change due), and VendingMachine (aggregate root). Define relationships and invariants.

3. Design User-Facing API

Specify public methods: selectProduct(code), insertMoney(amount), dispenseProduct(), returnChange(), cancelTransaction(). Ensure API is intuitive and handles errors (e.g., insufficient funds, out of stock).

4. Model Explicit State Machine

Define states: Idle, HasMoney, ProductSelected, Dispensing, OutOfStock, Maintenance. Specify transitions (e.g., Idle -> HasMoney on insertMoney, HasMoney -> Dispensing on selectProduct with sufficient funds). Use State pattern to implement.

5. Apply Design Patterns and Discuss Trade-offs

Use State for state transitions, Strategy for payment/change algorithms, Factory for creating products or states, and Singleton for the vending machine instance if appropriate. Discuss trade-offs: e.g., State pattern vs. switch statements, extensibility vs. complexity.

Key Points to Mention

  • Encapsulation of state-specific behavior using the State pattern to avoid complex conditionals.
  • Use of Strategy pattern for interchangeable payment and change-making algorithms.
  • Factory pattern for creating products or states, promoting loose coupling.
  • Singleton pattern for the vending machine if only one instance is needed, but discuss its drawbacks.
  • Clear separation between domain model and user-facing API, adhering to SOLID principles.
  • Handling edge cases: exact change, out-of-stock, insufficient funds, and transaction cancellation.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

How would you write unit tests for this vending machine? Cover happy-path purchases, exact change, insufficient funds, sold-out items, cancel/refund, and concurrent purchases.

System DesignTechnical Trade-offs
Author's notes

This is where I got humbled a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the vending machine's interface and state model, then structure tests around the six scenarios using a table-driven approach. For each scenario, define the initial state, actions, and expected outcomes, and include edge cases like exact change and concurrency. Emphasize deterministic tests with mocks for external dependencies and discuss trade-offs in test coverage.

Pro tip: Demonstrate maturity by discussing how to test concurrency deterministically—use synchronization primitives or dependency injection to control timing, and avoid flaky tests by isolating state. Also, mention that unit tests should be fast and independent, so consider using in-memory fakes for hardware components.

1. Clarify Requirements and Interface

Ask clarifying questions about the vending machine's API, state transitions, and expected behaviors for each scenario. Define the system under test and identify dependencies to mock.

2. Design Test Cases for Each Scenario

For each of the six scenarios, outline specific test cases covering normal and edge conditions. Use a table to map inputs to expected outputs and state changes.

3. Implement Tests with Isolation

Write unit tests using a framework like JUnit or pytest, mocking external components (e.g., coin dispenser, inventory). Ensure each test is independent and deterministic.

4. Address Concurrency and Edge Cases

For concurrent purchases, design tests that simulate multiple threads or use dependency injection to control interleaving. Verify that the system handles race conditions correctly.

5. Discuss Trade-offs and Coverage

Explain choices like using fakes vs. mocks, test granularity, and how to balance coverage with maintainability. Mention any limitations of unit tests and the need for integration tests.

Key Points to Mention

  • Use a state machine model to represent the vending machine's behavior and derive test cases from state transitions.
  • For exact change, test scenarios where the machine has limited coins and must provide change optimally.
  • For insufficient funds, verify that the machine rejects the purchase and returns inserted coins without side effects.
  • For sold-out items, ensure the machine does not dispense and handles refunds correctly.
  • For cancel/refund, test that the machine returns the exact amount inserted and resets state.
  • For concurrent purchases, use synchronization or transactional semantics to prevent double-spending and ensure inventory consistency.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.