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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Convert Units Between Measures
00:00
5 left

Convert Units Between Measures

MediumPython

Problem

Granular field data may report measurements such as acres, hectares, feet, and meters. Given known conversion relationships, implement a function that converts a value from one unit to another, even when no direct relationship is provided.

Model each unit as a graph node. A conversion A -> B with factor f means one unit of A equals f units of B. The reverse relationship has factor 1 / f. Return None when the source and target units are disconnected.

Formal Specification

Implement convert_units(value, source, target, conversions).

  • value is a real number.
  • source and target are non-empty unit-name strings.
  • conversions is a list of three-element lists [unit_a, unit_b, factor], where factor is positive and means 1 unit_a = factor unit_b.
  • Return a numeric value representing value in target units. Return value unchanged when source == target. Return None if no path exists.
  • Assume conversion relationships are mutually consistent when multiple paths exist.

Constraints

  • 1 <= len(conversions) <= 10^4
  • Each conversion contains two unit names and one positive factor
  • 0 < factor <= 10^12
  • Conversion factors and values may be fractional
  • Return None when source and target are disconnected

Function Signature

def convert_units(value, source, target, conversions):
Interviewer

Your question is Convert Units Between Measures. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.