← Questions
Order manager with partial cancellations and indexed queries
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's onsite coding (debug, integrate, build) round, last asked Mar 2026. It tests design, hash map and sorting.
Build an in-memory order manager driven by text commands. It creates orders, cancels part or all of an order's quantity, and answers queries by status and by creation time. Return one result string per command.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
process(operations: list[str]) -> list[str]with these commands:-
CREATE order_id user_id created_at quantity— check the user ID first, then the timestamp, then that the order ID is new. ReturnsCREATED,INVALID_USER_ID,INVALID_TIMESTAMPorDUPLICATE_ORDER. -
CANCEL order_id quantity— cancels that many units. ReturnsPARTIALLY_CANCELLEDwhile some quantity remains,CANCELLEDwhen none is left,ORDER_NOT_FOUND, orINVALID_CANCELif the quantity is more than what remains. -
ORDERS_BY_STATUS ACTIVE|CANCELLED— matching order IDs, comma-separated, in creation order (empty string if none). An order is active while its remaining quantity is positive. -
ORDERS_BETWEEN start end— every order created in the inclusive time range, including cancelled ones, sorted by creation time and then by creation order. ReturnsINVALID_RANGEif a timestamp is invalid orstart > end.
-
- A rejected command changes nothing.
- A valid user ID is 1 to 32 ASCII letters, digits,
_or-. A valid timestamp is a real UTC time in exactly the formYYYY-MM-DDTHH:mm:ssZ.
Example
CREATE o1 user_1 2026-03-20T09:00:00Z 5 -> CREATED
CREATE o2 user-2 2026-03-20T10:00:00Z 3 -> CREATED
CANCEL o1 2 -> PARTIALLY_CANCELLED
ORDERS_BY_STATUS ACTIVE -> o1,o2
ORDERS_BETWEEN 2026-03-20T00:00:00Z 2026-03-20T23:59:59Z -> o1,o2
CANCEL o1 3 -> CANCELLED
ORDERS_BY_STATUS CANCELLED -> o1
ORDERS_BY_STATUS ACTIVE -> o2
CREATE a bad!user 2026-03-21T00:00:00Z 1 -> INVALID_USER_ID
CREATE b user_b 2026-02-30T00:00:00Z 1 -> INVALID_TIMESTAMP # no February 30
Constraints
- Up to 100,000 commands. Quantities are positive and at most 10^12.
- Keep indexes for status and time; do not scan every order on each query.
Clarifying questions
- Can a cancelled order be reactivated?
- Can creation times arrive out of order?
- Is a partially cancelled order listed as active?
- Should timestamps with offsets other than
Zbe accepted?
What a strong answer covers
- Validation in the stated order, with a strict timestamp parser that rejects impossible dates.
- An order record (original and remaining quantity, status) plus a status index (insertion-ordered sets) updated on each cancel.
- A sorted index of
(created_at, sequence, order_id)and binary search for range queries: O(log n + k). - Integer arithmetic for quantities up to 10^12.
- Tests for each error code and for orders created out of time order.
Follow-ups
- Persist the orders in a database. Which indexes do the two queries need?
- Optimize
ORDERS_BY_STATUSwhen there are millions of active orders and the result is paginated. - Add per-user queries.
Where it's asked
- Stripe · Onsite coding: debug, integrate, build · Last asked Mar 202660 min · Project editor with starter code and tests: fix bugs in an unfamiliar codebase, wire an external API spec into a service, or complete a skeleton in parts · Varies by session: some onsites have one AI-assisted session and an integration session without AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
