Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Currency Conversion Queries

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Currency Conversion Queries. Start with the requirements on the right.

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.

Problem

An Axon cloud platform service receives currency exchange rates and must answer multiple conversion queries. Each exchange rate defines a directed conversion, and the reverse conversion is also available at the reciprocal rate.

Implement convert_currency(rates, queries) to return the conversion multiplier for each query. Return -1.0 when no conversion path exists.

Formal Specification

  • rates is a list of [sourceCurrency, targetCurrency, rate], where currencies are non-empty strings and rate is a positive number.
  • queries is a list of [sourceCurrency, targetCurrency] pairs.
  • Return a list of floating-point values in query order.
  • A query from a currency to itself returns 1.0 if that currency appears in the exchange-rate graph. Otherwise, return -1.0.
  • If a path contains rates a -> b and b -> c, its conversion multiplier is the product of those rates.

Constraints

  • 1 <= len(rates), len(queries) <= 10^4
  • Each currency name contains at most 8 uppercase letters
  • 0 < rate <= 10^6
  • Each rate pair has distinct currencies
  • The exchange graph can be disconnected

Function Signature

def convert_currency(rates, queries):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output