Implement idempotent request handling with idempotency keys
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates understanding of idempotency semantics, idempotency-key handling, concurrency control, synchronization, and in-memory TTL-based caching for API request processing, and it belongs to the Coding & Algorithms domain with an emphasis on practical application.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: (10, [[0,'k1','charge'], [1,'k1','charge-again'], [2,'','no-key'], [11,'k1','after-ttl']])
Expected Output: {'responses': [{'statusCode': 200, 'body': 'processed:charge:1'}, {'statusCode': 200, 'body': 'processed:charge:1'}, {'statusCode': 200, 'body': 'processed:no-key:2'}, {'statusCode': 200, 'body': 'processed:after-ttl:3'}], 'processCount': 3}
Explanation: Repeated key reuses response until expiry.
Input: (5, [[0,None,'a'], [1,None,'a']])
Expected Output: {'responses': [{'statusCode': 200, 'body': 'processed:a:1'}, {'statusCode': 200, 'body': 'processed:a:2'}], 'processCount': 2}
Explanation: Missing key always executes.
Input: (5, [[0,'a','first'], [4,'a','repeat'], [5,'a','expired']])
Expected Output: {'responses': [{'statusCode': 200, 'body': 'processed:first:1'}, {'statusCode': 200, 'body': 'processed:first:1'}, {'statusCode': 200, 'body': 'processed:expired:2'}], 'processCount': 2}
Explanation: Expiry is at timestamp >= first + ttl.
Hints
- Choose a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.