Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Refactor Using Design Patterns

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

Your question is Refactor Using Design Patterns. 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

Refactor a hypothetical repository containing unused methods and fields to improve code structure using design patterns like factory providers.

Asked in the Technical Screening stage. First part of a 60-minute technical assessment allocated 30 minutes.

Implement a refactoring pass over a repository model. Given classes, fields, methods, and provider mappings, remove unused members and replace direct construction with factory-based creation where applicable.

Input

  • classes: list of class definitions
  • usage: list of method and field references that are actually used
  • providers: mapping from class names to factory provider names

Output

Return the cleaned repository structure with unused members removed and object creation routed through providers when a mapping exists.

Constraints

  • 1 <= len(classes) <= 10^4
  • 0 <= total number of fields and methods across all classes <= 10^5
  • 0 <= len(usage) <= 10^5
  • Class, field, method, and provider names are non-empty strings
  • Each class definition contains unique field names and unique method names

Function Signature

def refactor_repository(classes, usage, providers):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output