Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Create Object With Getters Setters

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

Your question is Create Object With Getters Setters. 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

Implement a CgiServiceConfig object for configuring a CGI Nederland service. The object must keep its attributes private, expose setters and getters, validate every assigned value, and apply a sequence of updates in order.

Write build_service_config(initial, updates). It must create a configuration object from the initial values, apply each update through the appropriate setter, and return a dictionary containing the final values obtained through getters.

Formal specification

  • initial is a dictionary with keys service_name, timeout, retries, and endpoints.
  • service_name is a non-empty string.
  • timeout is a positive integer measured in seconds.
  • retries is an integer from 0 through 5.
  • endpoints is a list of unique, non-empty strings.
  • updates is a list of two-item lists, where each item contains a field name and replacement value. Updates are applied sequentially, so later updates replace earlier ones.
  • Valid field names are service_name, timeout, retries, and endpoints.
  • The function returns a dictionary with the same four keys in that order.
  • Invalid values or unknown field names must raise ValueError.

Examples

Example 1

initial = {"service_name": "payments", "timeout": 30, "retries": 2, "endpoints": ["api.cgi.nl"]}
updates = [["timeout", 60], ["retries", 4]]
Output: {"service_name": "payments", "timeout": 60, "retries": 4, "endpoints": ["api.cgi.nl"]}

The updates are validated and applied in their listed order.

Example 2

initial = {"service_name": "portal", "timeout": 10, "retries": 0, "endpoints": ["web.cgi.nl", "mobile.cgi.nl"]}
updates = [["endpoints", ["web.cgi.nl"]]]
Output: {"service_name": "portal", "timeout": 10, "retries": 0, "endpoints": ["web.cgi.nl"]}

The endpoint list is replaced through its setter.

Constraints

  • 1 <= len(service_name) <= 100
  • 1 <= timeout <= 3600
  • 0 <= retries <= 5
  • 1 <= len(endpoints) <= 100
  • 0 <= len(updates) <= 10^4

Constraints

  • 1 <= len(service_name) <= 100
  • 1 <= timeout <= 3600
  • 0 <= retries <= 5
  • 1 <= len(endpoints) <= 100
  • 0 <= len(updates) <= 10^4
  • Endpoint strings are unique in every valid configuration

Function Signature

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