Propagate runtime ID across API requests
Propagate Runtime ID Across Chained API Requests
You are given a sequence of 5 API requests that must be executed in order. The first request (a POST) succeeds and returns 200 OK with a JSON response containing a newly created resource ID. Requests 2 through 5 currently fail because they incorrectly use a hardcoded ID (e.g., "123") in their URL path and/or JSON body instead of the ID returned by Request 1.
Task
Write a function that, given:
requests: A list of request objects in execution order. Each request object has the following fields:method(string)url(string)headers(map of string to string)body(can benull, a JSON object, or a JSON string)
first_response_body: The parsed JSON body returned by Request 1, which includes the runtime ID (for example, under the keyid).old_id: The hardcoded ID value currently present in later requests that needs to be replaced.
Your function should return a new list of requests where, for Requests 2 through 5:
- Every occurrence of
old_idin the request URL is replaced with the runtime ID extracted fromfirst_response_body. - Every occurrence of
old_idanywhere inside the request body (including nested fields, arrays, and string values) is replaced with the runtime ID.
Requirements / Notes
- Do not modify Request 1.
- The runtime ID may be numeric or string; preserve the correct type when updating JSON bodies.
- The request body may be
null, a JSON object, or a JSON string. If it's a JSON string, you should parse it, modify it, and then serialize it back into a string. - Assume all requests are well-formed; focus on correct propagation and replacement logic.
Output
Return the updated list of request objects.
AI interview prep
Core dilemma
The central engineering conflict is implementing a robust, precise, and type-aware transformation across heterogeneous data structures (URLs, JSON objects, JSON strings, arrays) to replace a specific value, while handling nested complexity and potential edge cases without introducing unintended side effects.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Propagate runtime ID across API requests
Propagate Runtime ID Across Chained API Requests
You are given a sequence of 5 API requests that must be executed in order. The first request (a POST) succeeds and returns 200 OK with a JSON response containing a newly created resource ID. Requests 2 through 5 currently fail because they incorrectly use a hardcoded ID (e.g., "123") in their URL path and/or JSON body instead of the ID returned by Request 1.
Task
Write a function that, given:
requests: A list of request objects in execution order. Each request object has the following fields:method(string)url(string)headers(map of string to string)body(can benull, a JSON object, or a JSON string)
first_response_body: The parsed JSON body returned by Request 1, which includes the runtime ID (for example, under the keyid).old_id: The hardcoded ID value currently present in later requests that needs to be replaced.
Your function should return a new list of requests where, for Requests 2 through 5:
- Every occurrence of
old_idin the request URL is replaced with the runtime ID extracted fromfirst_response_body. - Every occurrence of
old_idanywhere inside the request body (including nested fields, arrays, and string values) is replaced with the runtime ID.
Requirements / Notes
- Do not modify Request 1.
- The runtime ID may be numeric or string; preserve the correct type when updating JSON bodies.
- The request body may be
null, a JSON object, or a JSON string. If it's a JSON string, you should parse it, modify it, and then serialize it back into a string. - Assume all requests are well-formed; focus on correct propagation and replacement logic.
Output
Return the updated list of request objects.