Prepin
Log in

Based on a list of conversion items between various symbols, create an algorithm that provides the conversion ratio for each symbol pair.

Approach / Explanation

This question requires you to create a function or algorithm that takes a list of conversion rates between various symbol pairs and provides the conversion ratio between two specified symbols. To grasp the question effectively, consider this example: imagine you have a list of conversion rates for different currencies, such as 1 USD = 0.88 EUR. If you need to determine the conversion rate from USD to EUR, your function should return this rate. Important aspects to keep in mind while addressing this question include: You must design a data structure capable of storing multiple conversion rates from different symbols. The algorithm should be able to accurately fetch the conversion rate for a specified pair of symbols. Be mindful of potential edge cases, such as situations where no conversion rate exists between the requested symbols.

Suggested Answer

For this example, we'll utilize Python because of its ease of use and flexibility. We'll begin by creating a dictionary to hold the conversion rates, followed by defining a function to retrieve and return the desired conversion rate. To set up initially: conversion_dict = { "USD_EUR" : 0.88 , "USD_GBP" : 0.77 , "GBP_EUR" : 1.13 } Next, we will define a function: def get_conversion ( symbol1 , symbol2 ) : pair = f' { symbol1 } _ { symbol2 } ' reverse_pair = f' { symbol2 } _ { symbol1 } ' if pair in conversion_dict : return conversion_dict [ pair ] elif reverse_pair in conversion_dict : # Return the reciprocal conversion if reverse pair is available return 1 / conversion_dict [ reverse_pair ] else : return "Conversion rate not available" You can use this function by calling get_conversion('USD', 'EUR') to retrieve the conversion rate from USD to EUR.

Alternative Answer

This task can also be tackled using an object-oriented approach, utilizing "Symbol" objects along with a "Conversion" object to handle them: public class Symbol { String name ; Map < Symbol , Double > conversionRates = new HashMap < > ( ) ; public Symbol ( String name ) { this . name = name ; } public addConversion ( Symbol symbol , double rate ) { conversionRates . put ( symbol , rate ) ; } } public class Conversion { Map < String , Symbol > symbols = new HashMap < > ( ) ; public addConversion ( String sym1 , String sym2 , double rate ) { if ( ! symbols . containsKey ( sym1 ) ) { symbols . put ( sym1 , new Symbol ( sym1 ) ) ; } if ( ! symbols . containsKey ( sym2 ) ) { symbols . put ( sym2 , new Symbol ( sym2 ) ) ; } symbols . get ( sym1 ) . addConversion ( symbols . get ( sym2 ) , rate ) ; } public double getConversion ( String sym1 , String sym2 ) { if ( ! symbols . containsKey ( sym1 ) || ! symbols . containsKey ( sym2 ) ) { return - 1 ; // or throw an exception } Symbol symbol1 = symbols . get ( sym1 ) ; Symbol symbol2 = symbols . get ( sym2 ) ; if ( symbol1 . conversionRates . containsKey ( symbol2 ) ) { return symbol1 . conversionRates . get ( symbol2 ) ; } if ( symbol2 . conversionRates . containsKey ( symbol1 ) ) { return 1 / symbol2 . conversionRates . get ( symbol1 ) ; } return - 1 ; // or throw an exception } } You can now easily add conversion rates using addConversion("USD", "EUR", 0.88) and retrieve conversion rates with getConversion("USD", "EUR") .

Question Details

Difficulty & Category

Medium
algorithms

Companies

Snap

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.