Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Immutable vs Mutable Classes
00:00
5 left

Immutable vs Mutable Classes

MediumPython

Problem

Globallogic services often pass employee profiles between components that may update address records independently. Design a mutable Address class and an immutable Employee class that prevents external mutation of employee identity and stored address state.

Requirements

  1. Address must store street and city, and allow both fields to be changed after construction.
  2. Employee must store employee_id, name, and an Address.
  3. Once constructed, an Employee must not allow reassignment of employee_id, name, or its internal address reference.
  4. The employee must make a defensive copy of the supplied Address, so later changes to the caller's address do not affect the employee.
  5. Accessing employee.address must return another defensive copy, so callers cannot mutate the employee through the returned object.
  6. Implement create_employee(employee_id, name, address_data), which constructs an employee from an address dictionary and returns its dictionary snapshot using to_dict().

Formal Specification

Input consists of an integer employee_id, a string name, and a dictionary containing string keys street and city. The helper returns a dictionary with keys employee_id, name, street, and city. The classes must enforce the immutability and defensive-copying rules above.

Constraints

  • employee_id is an integer
  • name, street, and city are non-empty strings
  • Each string has at most 200 characters
  • address_data contains the keys street and city

Function Signature

def create_employee(employee_id, name, address_data):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.