Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Validate API Response Data Points

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Validate API Response Data Points. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In a Capgemini API testing workflow, an automated check must verify selected data points in a JSON response. Implement a function that confirms every requested path exists and contains the expected value, including values nested inside dictionaries and lists.

Formal Specification

Write validate_response(response, checks).

  • response is a JSON-compatible Python object containing dictionaries, lists, strings, numbers, booleans, or None.
  • checks is a list of dictionaries. Each dictionary contains:
    • path: a non-empty list of string dictionary keys or integer list indexes.
    • expected: the required value at that path.
  • Return True only when every path exists and its value equals expected with the same Python type. For example, True must not match 1.
  • Return False if a path is missing, an index is invalid, an intermediate value has the wrong container type, or any value does not match.
  • An empty checks list is valid and should return True.

Constraints

  • 0 <= len(checks) <= 10^4
  • 1 <= len(path) <= 50 for every check
  • Each path segment is a string key or a non-negative integer list index
  • The response contains at most 10^5 total JSON nodes
  • Values must match both by type and by value

Function Signature

def validate_response(response, checks):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output