Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Polymorphism in Real-World Code
00:00
5 left

Polymorphism in Real-World Code

HardPython

Problem

Write a program to demonstrate polymorphism in a real-world scenario.

Implement process_documents(documents) using a shared document interface and separate subclasses for invoices, forms, and contracts. Each subclass must override the same processing method, and the main function must process all inputs polymorphically without type-specific calculation logic in its iteration loop.

Input is a list of dictionaries. Each dictionary contains kind and fields required by that kind. Return an object containing per-document results and the total cost.

Supported calculations: invoices cost pages * 2 plus 5 for urgent priority, forms cost pages + attachments * 2, and contracts cost pages * 3 + clauses.

Constraints

  • 0 <= len(documents) <= 10000
  • Each document kind is one of: invoice, form, contract
  • Invoice pages and form pages are nonnegative integers
  • Invoice priority is either normal or urgent
  • Form attachments and contract clauses are nonnegative integers

Function Signature

def process_documents(documents):
Interviewer

Your question is Polymorphism in Real-World Code. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.