coding•Data Structures & Algorithms•High-Concurrency Systems & Atomic Operations
Flash Sale Inventory Decrement: Redis Lua Script Concurrency
Problem Statement
During a major flash sale, thousands of customers attempt to purchase an item with limited stock.
Simulate atomic inventory deduction:
Given an `initial_stock` and a list of purchase quantity requests:
- Process requests sequentially.
- If the requested quantity is $\le$ remaining stock, approve the request and decrement stock.
- Otherwise, reject the request without changing remaining stock.
Return the total number of successfully approved orders and the final remaining stock.
### Input Format
- First line contains an integer `initial_stock`.
- Second line contains space-separated integers representing requested quantities.
### Output Format
- Print two space-separated integers: `successfulOrders remainingStock`.
### Constraints
- `0 <= initial_stock <= 10^6`
- `1 <= requests.length <= 10^5`
- `1 <= requests[i] <= 100`
Sample Cases
Example 1Sample Case
Input: 10
3 5 4 2
Output: 3 0
Example 2Sample Case
Input: 10
3
Output: 1 7
Example 3Sample Case
Input: 10
10
Output: 1 0
Example 4Sample Case
Input: 5
2
Output: 1 3
Constraints
Runtime Limit: 2000 ms per test batch
Memory Limit: 256 MB
Standard I/O stream reading
Official submissions are evaluated against hidden test cases on the server to verify time & memory complexity.