PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Implement idempotent request handling with idempotency keys

Last updated: Mar 29, 2026

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.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Feb 11, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
4
0
Loading...

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.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
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.