Oversell Safe Stock Reservation For A Flash Sale

Last updated: November 29, 2026

Quick Overview

Implement a thread-safe stock holder for a single flash-sale SKU that lets each user reserve at most one unit, holds the unit for a payment window, returns expired holds to stock, and never sells more units than exist.

Pinduoduo
Software Engineering Fundamentals
Software Engineer
Pinduoduo
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Implement a thread-safe stock holder for a single flash-sale SKU that lets each user reserve at most one unit, holds the unit for a payment window, returns expired holds to stock, and never sells more units than exist.

Pinduoduo runs limited-quantity flash sales where tens of thousands of requests hit the same SKU in the first second, so overselling or double-buying is a real business risk. The interviewer checks that you understand races in check-then-act code, keep critical sections small, and can translate the single-process design into Redis or database primitives.

What the Interviewer Expects
  • Identifies the check-then-decrement race and protects it with one lock
  • Enforces one unit per user inside the same critical section
  • Reclaims expired holds so abandoned carts return stock
  • Makes the clock injectable so timeouts are testable
  • Explains the distributed version with an atomic Redis script or a conditional UPDATE
Key Topics to Cover
concurrency
locks
idempotency
inventory
Redis
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you implement reserve as a single atomic operation in Redis?
  • The SQL version uses UPDATE stock SET n = n - 1 WHERE sku = ? AND n > 0. What are its limits under heavy contention?
  • How do you make confirm_payment idempotent when the payment callback is retried?
  • How would you shard one hot SKU across several counters?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements

reserve(user) returns RESERVED, SOLD_OUT or DUPLICATE. A reservation holds one unit for hold_seconds. confirm_payment(user) turns a live hold into a sale and fails if the hold expired. Invariant...

High-Level Design

The bug to avoid is if available > 0: available -= 1 executed by two threads at once. Put the reclaim, the duplicate check, the availability check and the decrement under one lock so they are atomic...


Submit Your Answer
Markdown supported

Related Questions