PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • hard
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Implement idempotent request handling with idempotency keys

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

## Problem You are implementing an API endpoint (e.g., `POST /charge`) that must be **idempotent** using an **Idempotency-Key**. Design and implement an in-memory component that enforces the following behavior: ### API you must implement Implement a class/function with an interface equivalent to: - `Response handle(Request req)` Where `Request` contains: - `string idempotencyKey` (may be empty/null) - `string payload` (request body) And `Response` contains: - `int statusCode` - `string body` ### Requirements 1. **No idempotency key:** If `idempotencyKey` is missing, process the request normally (always execute the business logic). 2. **First time a key is seen:** Execute the business logic exactly once, return its `Response`, and store the response associated with this key. 3. **Repeated key:** If the same `idempotencyKey` is seen again, return the **same stored response** and **do not** execute the business logic again. 4. **Concurrent duplicates:** If two requests with the same key arrive concurrently, ensure the business logic runs **only once**; the other request(s) should wait and then return the same stored response. 5. **TTL expiration:** Stored keys expire after `ttlSeconds`. After expiry, the next request with that key should be treated as a new key. ### Inputs/Constraints (for testing) - Up to `10^5` calls to `handle`. - `ttlSeconds` is provided at construction time. - You may assume a single process (in-memory only), but multiple threads may call `handle` concurrently. ### What to return Implement the logic described above. You may stub the business logic as a provided callback `Response process(payload)` that you call only when needed.

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.

Simulate idempotent request handling with TTL. Requests are [time, key, payload]; an empty or None key always executes business logic. Return responses and total business executions.

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

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)