Skip to main content
All Questions

Given a list of conversion items between symbols, write an algorithm to return the conversion ratio for each pair of symbols.

Hard
Unlock detailed company stats for this questionUpgrade

Calculate Conversion Ratios. Given a list of conversion pairs between different units of measurement, write an algorithm that returns the conversion ratio from one unit to another, either directly or through a series of conversions. If no conversion is possible, the algorithm should return None.

For instance, if you have the following conversions: ("USD", "EUR", 0.88), ("EUR", "JPY", 129.53), and ("GBP", "USD", 1.39), the algorithm should be able to deduce the conversion rate from "USD" to "JPY" as well as from "GBP" to "EUR".

Examples:

conversions = [ ('USD', 'EUR', 0.88), ('EUR', 'JPY', 129.53), ('GBP', 'USD', 1.39) ] source = 'USD' destination = 'JPY' output: 114.0064 # 0.88 * 129.53 source = 'GBP' destination = 'EUR' output: 1.2232 # 1.39 / 0.88 source = 'EUR' destination = 'GBP' output: None # No direct or indirect conversion available

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.