Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
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
Polymorphic Trade Validation in Python
00:00
5 left

Polymorphic Trade Validation in Python

MediumPython

Problem

DTCC CTM receives trade instructions for multiple asset classes. Implement an object-oriented validator that uses abstraction and polymorphism to apply the correct validation rules without placing all asset-specific logic in one conditional-heavy function.

Create an abstract TradeRule class with a validate(trade) method. Implement EquityRule, BondRule, and OptionRule subclasses. The validate_trades function must select the appropriate rule for each instruction and return validation results in the original order.

Formal Specification

Input trades is a list of dictionaries. Every dictionary contains id, asset_type, quantity, price, and settlement_days. Option trades also contain strike.

Return a list of dictionaries with id, valid, and errors. valid is true only when no rules are violated. Error messages must use the exact strings defined below.

  • Equity: quantity must be positive, price must be positive, and settlement must be 1 or 2 days.
  • Bond: quantity must be positive, price must be between 0 and 100 inclusive, and settlement must be between 1 and 3 days.
  • Option: quantity must be positive, price must be nonnegative, strike must be positive, and settlement must be exactly 1 day.
  • Unknown asset types produce one error: Unknown asset type.

Constraints

  • 1 <= len(trades) <= 10^4
  • Each trade contains the required fields for its asset type
  • Numeric values fit standard Python integer or floating-point ranges
  • Asset types are case-sensitive
  • Output order must match input order

Function Signature

def validate_trades(trades):
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.