HomeInterview QuestionsHow would you approach a problem where you need to…

How would you approach a problem where you need to ensure that the user receiver ID is the same for one request? Provide a simple approach and code.

🟡 Medium Coding Junior level
1Times asked
Apr 2026Last seen
Apr 2026First seen

💡 Model Answer

To guarantee that the user receiver ID remains consistent within a single request, you can validate the ID at the entry point of your API or service. The typical approach is:

  1. Extract the ID from the request payload or headers.
  2. Validate the format (e.g., UUID, numeric) and ensure it exists in your database.
  3. Store the ID in a request‑scoped context (e.g., a thread‑local variable, request object, or context map) so that all downstream handlers can access the same value.
  4. Enforce immutability by marking the context variable as read‑only or by not exposing mutator methods.
  5. Return an error if any part of the request attempts to change the ID.

Example in Python using Flask:

python
from flask import Flask, request, g
app = Flask(__name__)

@app.before_request
def validate_receiver_id():
    receiver_id = request.json.get("receiver_id")
    if not receiver_id or not is_valid_uuid(receiver_id):
        return {"error": "Invalid receiver_id"}, 400
    # Store in request‑scoped context
    g.receiver_id = receiver_id

@app.route('/process', methods=['POST'])
def process():
    # All handlers can safely use g.receiver_id
    return {"status": "ok", "receiver_id": g.receiver_id}

Complexity: O(1) for validation and storage; the approach scales linearly with the number of requests. This pattern ensures consistency and prevents accidental ID changes during request processing.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500