← 返回 jpmorgan 的题目列表Code Review & Refactor: fix SRP violations, remove global state, and make it concurrency-safe
类型:online_judge
Coding: Code review & refactor (SRP / concurrency / global variables)
You are given a Python snippet that processes orders. The code has issues such as:
SRP violation: one function does validation, pricing, persistence, and notifications.
Global mutable state for counters/logging.
Race conditions under concurrency (multiple threads processing orders), causing duplicated IDs, incorrect counters, and corrupted logs.
Tasks:
Identify at least 4 major issues and explain the risks.
Refactor without changing external behavior:
Remove global mutable state.
Split responsibilities (validation, pricing, persistence, notification).
Make it correct under multi-threaded concurrent processing (unique order IDs, accurate counts).
I/O
Input: first line integer n; next n lines are orders userId,amount.
Output:
count=<n>
n lines of orderId=<id> (sorted ascending is fine)
IDs must be globally unique.
Constraints
1 <= n <= 20000
1 <= amount <= 1e9
Example
Input:
3
u1,10
u2,20
u3,30
Output:
count=3
orderId=100000
orderId=100001
orderId=100002
Example
Input
1
u1,10
Output
count=1
orderId=100000