PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of state management and finite-state machines, API design for lifecycle control, and implementation of in-memory data structures and transition validation in the Coding & Algorithms category.

  • medium
  • Coinbase
  • Coding & Algorithms
  • Software Engineer

Design crypto trading order control API

Company: Coinbase

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are implementing part of a simple crypto trading system that manages client orders. Each order can be in one of several states: `NEW`, `ACTIVE`, `PAUSED`, `CANCELLED`, or `FILLED`. You need to support the following operations for **a single order** (you can assume order creation is already done): 1. `pause(orderId)`: Temporarily pause an active order so that it will not be matched or executed while paused. 2. `resume(orderId)`: Resume a paused order so that it becomes active again and can be matched. 3. `cancel(orderId)`: Cancel an order so that it will never be executed in the future. Rules and constraints: - Valid state transitions are: - `NEW -> ACTIVE` - `ACTIVE -> PAUSED` - `PAUSED -> ACTIVE` (on resume) - `ACTIVE -> CANCELLED` - `PAUSED -> CANCELLED` - `ACTIVE -> FILLED` - Once an order is in `CANCELLED` or `FILLED`, no further operations (`pause`, `resume`, or `cancel`) should change its state. - `pause` on a non-`ACTIVE` order should be rejected. - `resume` on a non-`PAUSED` order should be rejected. - `cancel` on a `CANCELLED` or `FILLED` order should be a no-op or return an error (choose one behavior and be consistent). Implement a small in-memory component that: - Stores the current state of each order by `orderId`. - Exposes methods `pause`, `resume`, and `cancel` that update state according to the rules above. - Returns success/failure for each operation based on whether the transition is allowed. Describe your data structures and implement the core logic for these operations.

Quick Answer: This question evaluates understanding of state management and finite-state machines, API design for lifecycle control, and implementation of in-memory data structures and transition validation in the Coding & Algorithms category.

You are building a small in-memory component for a crypto trading system. Each order has an `orderId` and a current state. For this problem, you must process a list of API calls and update order states according to the allowed transitions. Possible states are: `NEW`, `ACTIVE`, `PAUSED`, `CANCELLED`, and `FILLED`. Supported operations: - `pause(orderId)`: allowed only when the order is currently `ACTIVE`, changing it to `PAUSED` - `resume(orderId)`: allowed only when the order is currently `PAUSED`, changing it to `ACTIVE` - `cancel(orderId)`: allowed only when the order is currently `ACTIVE` or `PAUSED`, changing it to `CANCELLED` Rules: - Once an order becomes `CANCELLED` or `FILLED`, no later operation may change it. - Any invalid operation must return `False` and leave the state unchanged. - If an operation references an unknown `orderId`, it also returns `False`. - `ACTIVE -> FILLED` exists in the broader trading system, but this problem does not include a `fill` operation. A `FILLED` state may appear only in the initial data. Given the initial list of orders and a list of operations, return: 1. A list of booleans showing whether each operation succeeded 2. The final state of every order in the same order as the input `orders` list

Constraints

  • 0 <= len(orders) <= 200000
  • 0 <= len(operations) <= 200000
  • Each `orderId` in `orders` is unique
  • Each initial state is one of `NEW`, `ACTIVE`, `PAUSED`, `CANCELLED`, `FILLED`
  • Each operation action is one of `pause`, `resume`, or `cancel`

Examples

Input: ([('o1', 'ACTIVE'), ('o2', 'PAUSED'), ('o3', 'FILLED')], [('pause', 'o1'), ('resume', 'o2'), ('cancel', 'o1'), ('cancel', 'o3')])

Expected Output: ([True, True, True, False], [('o1', 'CANCELLED'), ('o2', 'ACTIVE'), ('o3', 'FILLED')])

Explanation: o1 is paused, o2 is resumed, then o1 is cancelled from PAUSED. Cancelling a FILLED order fails.

Input: ([('a', 'NEW'), ('b', 'CANCELLED'), ('c', 'ACTIVE')], [('pause', 'a'), ('resume', 'c'), ('cancel', 'b'), ('cancel', 'x'), ('cancel', 'c'), ('pause', 'c')])

Expected Output: ([False, False, False, False, True, False], [('a', 'NEW'), ('b', 'CANCELLED'), ('c', 'CANCELLED')])

Explanation: NEW cannot be paused, ACTIVE cannot be resumed, CANCELLED cannot be cancelled again, unknown order x fails, c is cancelled successfully, and then cannot be paused afterward.

Input: ([('btc', 'ACTIVE')], [('pause', 'btc'), ('resume', 'btc'), ('pause', 'btc'), ('cancel', 'btc'), ('resume', 'btc')])

Expected Output: ([True, True, True, True, False], [('btc', 'CANCELLED')])

Explanation: The order moves ACTIVE -> PAUSED -> ACTIVE -> PAUSED -> CANCELLED. After cancellation, resume fails.

Input: ([('z', 'PAUSED')], [])

Expected Output: ([], [('z', 'PAUSED')])

Explanation: With no operations, the state remains unchanged.

Input: ([], [('pause', 'ghost'), ('cancel', 'ghost')])

Expected Output: ([False, False], [])

Explanation: There are no stored orders, so every operation fails.

Solution

def solution(orders, operations):
    states = {}
    order_sequence = []

    for order_id, state in orders:
        states[order_id] = state
        order_sequence.append(order_id)

    transitions = {
        'pause': {'ACTIVE': 'PAUSED'},
        'resume': {'PAUSED': 'ACTIVE'},
        'cancel': {'ACTIVE': 'CANCELLED', 'PAUSED': 'CANCELLED'}
    }

    results = []

    for action, order_id in operations:
        if order_id not in states or action not in transitions:
            results.append(False)
            continue

        current_state = states[order_id]
        next_state = transitions[action].get(current_state)

        if next_state is None:
            results.append(False)
        else:
            states[order_id] = next_state
            results.append(True)

    final_states = [(order_id, states[order_id]) for order_id in order_sequence]
    return results, final_states

Time complexity: O(n + m), where n is the number of orders and m is the number of operations. Space complexity: O(n).

Hints

  1. A dictionary keyed by `orderId` lets you find and update an order's current state in O(1) time.
  2. Instead of writing many nested `if` statements, consider storing allowed transitions in a table like `(action, current_state) -> next_state`.
Last updated: May 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement an In-Memory Database - Coinbase (hard)
  • Implement a Coin-Constrained Jump Strategy - Coinbase (hard)
  • Implement Game Physics and Block Mining - Coinbase (hard)
  • Compute Total Manual Distance - Coinbase (medium)
  • Implement a Flappy Bird Jump Agent - Coinbase