Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Get started
Bubble Sort and OOP
00:00
5 left

Bubble Sort and OOP

EasyPython

Problem

Accion Labs needs a small in-memory utility to order records before displaying them in an internal engineering surface. Implement a stable bubble sort and demonstrate how inheritance and polymorphism can provide a consistent sorting interface for different record types.

Your implementation must sort a list of dictionaries in place by a named field. It must preserve the original relative order of records with equal keys, support ascending and descending order, and stop early when a complete pass makes no swaps.

Also define a base SortableRecord class with a sort_value() method, then create at least two subclasses that override this method. Explain how this override demonstrates polymorphism and how the subclasses inherit shared behavior from the base class.

Formal Specification

Implement bubble_sort(records, key, reverse=False), where records is a list of dictionaries and key is the field used for comparison. Return the same list object after sorting. Values for the selected field are comparable integers or strings.

Constraints

  • 0 <= len(records) <= 2,000
  • Every record contains the selected key
  • Selected values are mutually comparable integers or strings
  • The output must be the same list object as the input
  • Equal keys must retain their original relative order

Function Signature

def bubble_sort(records, key, reverse=False):
Interviewer

Your question is Bubble Sort and OOP. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.