Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
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

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.