Quick Overview

A coding problem that takes each currency's value in US dollars and returns the exchange rate for every ordered pair of distinct currencies, including USD, as FROM/TO:RATE strings. It tests handling a USD entry that may already be present, exact five-decimal formatting, and a deterministic sorted output order.

Format Exchange Rates for Every Ordered Currency Pair from USD Values

Company: Squarepoint

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a mapping from currency codes to their value in US dollars: `"SGD": 0.73` means one unit of SGD is worth 0.73 USD. Produce the exchange rate for every ordered pair of distinct currencies, including USD itself, as strings of the form `"FROM/TO:RATE"`, where RATE is how many units of TO one unit of FROM buys. Return the strings in sorted order. ### Function Signature ```python def currency_pair_rates(usd_values: dict[str, float]) -> list[str]: ``` ### Rules - USD is always one of the currencies and is worth exactly 1 USD, whether or not it appears in `usd_values`. If it does appear, its value is `1.0` and it still counts as one currency. - Include one string for every ordered pair `(FROM, TO)` of distinct currencies: both `A/B` and `B/A` appear, and no currency is paired with itself. - RATE is `usd_values[FROM] / usd_values[TO]`, with USD's value taken as `1.0`, computed in double precision and written with exactly five digits after the decimal point, rounded to the nearest such value, exactly as Python's `f"{rate:.5f}"` writes it: at least one digit before the decimal point, trailing zeros kept, and no sign, exponent or thousands separator. - Sort the strings in ascending order by character code (Python's default string ordering). Because every code is three uppercase letters, this is the same as sorting by FROM and then by TO. - If USD is the only currency, return an empty list. ### Constraints - `0 <= len(usd_values) <= 100` - Every key is exactly three uppercase English letters `A`–`Z`, and keys are distinct. - Every value `v` satisfies `0.01 <= v <= 100` and has at most six digits after the decimal point. If `"USD"` is a key, its value is `1.0`. - Every rate therefore lies between `0.0001` and `10000`. No exact rate lies within `1e-9` of a number exactly halfway between two consecutive five-decimal values, so correct rounding never depends on floating-point error. - With `m` distinct currencies including USD (at most 101), the output has `m * (m - 1)` strings, at most 10,100. ### Examples **Example 1** ```text Input: usd_values = {"SGD": 0.73} Output: ["SGD/USD:0.73000", "USD/SGD:1.36986"] ``` USD is added as the second currency. One SGD buys 0.73 USD, written with five decimals. One USD buys 1 / 0.73 = 1.369863... SGD, which rounds to 1.36986. **Example 2** ```text Input: usd_values = {"EUR": 1.1, "GBP": 1.25, "USD": 1.0} Output: ["EUR/GBP:0.88000", "EUR/USD:1.10000", "GBP/EUR:1.13636", "GBP/USD:1.25000", "USD/EUR:0.90909", "USD/GBP:0.80000"] ``` USD is already present and counts once, giving three currencies and six pairs. For example, GBP/EUR is 1.25 / 1.1 = 1.136363..., written as 1.13636, and USD/EUR is 1 / 1.1 = 0.909090..., written as 0.90909. **Example 3** ```text Input: usd_values = {"USD": 1.0} Output: [] ``` USD is the only currency, so there is no pair of distinct currencies.

Overview: A coding problem that takes each currency's value in US dollars and returns the exchange rate for every ordered pair of distinct currencies, including USD, as FROM/TO:RATE strings. It tests handling a USD entry that may already be present, exact five-decimal formatting, and a deterministic sorted output order.

Read the full Squarepoint Software Engineer interview experience this question came from

You are given a dictionary `usd_values` that maps currency codes to their value in US dollars: `"SGD": 0.73` means one unit of SGD is worth 0.73 USD. Produce the exchange rate for every ordered pair of distinct currencies, including USD itself, as strings of the form `"FROM/TO:RATE"`, where RATE is how many units of TO one unit of FROM buys. Return the strings in sorted order. ### Rules - USD is always one of the currencies and is worth exactly 1 USD, whether or not it appears in `usd_values`. If it does appear, its value is `1.0` and it still counts as one currency. - Include one string for every ordered pair `(FROM, TO)` of distinct currencies: both `A/B` and `B/A` appear, and no currency is paired with itself. - RATE is `usd_values[FROM] / usd_values[TO]`, with USD's value taken as `1.0`, computed in double precision and written with exactly five digits after the decimal point, rounded to the nearest such value, exactly as Python's `f"{rate:.5f}"` writes it: at least one digit before the decimal point, trailing zeros kept, and no sign, exponent or thousands separator. - Sort the strings in ascending order by character code (Python's default string ordering). Because every code is three uppercase letters, this is the same as sorting by FROM and then by TO. - If USD is the only currency, return an empty list. ### Language notes - The mapping arrives as a `dict` in Python, a plain object in JavaScript, a `java.util.Map<String, Double>` in Java and a `std::unordered_map<std::string, double>` in C++. Return a list (array) of strings. - Every value and rate is a floating-point number and nothing exceeds 2^31 - 1, so no 64-bit integer type is needed. Within these constraints JavaScript's `rate.toFixed(5)` and C++'s `"%.5f"` produce the same text as Python's `f"{rate:.5f}"`; in Java, format with `java.util.Locale.ROOT` so the decimal separator is always a period. ### Constraints - `0 <= len(usd_values) <= 100` - Every key is exactly three uppercase English letters `A`–`Z`, and keys are distinct. - Every value `v` satisfies `0.01 <= v <= 100` and has at most six digits after the decimal point. If `"USD"` is a key, its value is `1.0`. - Every rate therefore lies between `0.0001` and `10000`. No exact rate lies within `1e-9` of a number exactly halfway between two consecutive five-decimal values, so correct rounding never depends on floating-point error. - With `m` distinct currencies including USD (at most 101), the output has `m * (m - 1)` strings, at most 10,100. ### Example 1 ```text Input: usd_values = {"SGD": 0.73} Output: ["SGD/USD:0.73000", "USD/SGD:1.36986"] ``` USD is added as the second currency. One SGD buys 0.73 USD, written with five decimals. One USD buys 1 / 0.73 = 1.369863... SGD, which rounds to 1.36986. ### Example 2 ```text Input: usd_values = {"EUR": 1.1, "GBP": 1.25, "USD": 1.0} Output: ["EUR/GBP:0.88000", "EUR/USD:1.10000", "GBP/EUR:1.13636", "GBP/USD:1.25000", "USD/EUR:0.90909", "USD/GBP:0.80000"] ``` USD is already present and counts once, giving three currencies and six pairs. For example, GBP/EUR is 1.25 / 1.1 = 1.136363..., written as 1.13636, and USD/EUR is 1 / 1.1 = 0.909090..., written as 0.90909.

Constraints

  • 0 <= len(usd_values) <= 100
  • Every key is exactly three uppercase English letters A–Z, and keys are distinct.
  • Every value v satisfies 0.01 <= v <= 100 and has at most six digits after the decimal point. If "USD" is a key, its value is 1.0.
  • Every rate therefore lies between 0.0001 and 10000. No exact rate lies within 1e-9 of a number exactly halfway between two consecutive five-decimal values, so correct rounding never depends on floating-point error.
  • With m distinct currencies including USD (at most 101), the output has m * (m - 1) strings, at most 10,100.

Examples

Input: ({'SGD': 0.73},)

Expected Output: ['SGD/USD:0.73000', 'USD/SGD:1.36986']

Explanation: Source Example 1: one non-USD currency with USD implicit gives exactly two strings, one per direction; 1 / 0.73 = 1.369863... rounds to 1.36986.

Input: ({'EUR': 1.1, 'GBP': 1.25, 'USD': 1.0},)

Expected Output: ['EUR/GBP:0.88000', 'EUR/USD:1.10000', 'GBP/EUR:1.13636', 'GBP/USD:1.25000', 'USD/EUR:0.90909', 'USD/GBP:0.80000']

Explanation: Source Example 2: USD given explicitly counts once, so three currencies give six strings with no USD/USD.

Hints

  1. USD belongs to the set of currencies with value 1.0 in every input, whether or not it is a key. Make sure it is never counted twice.
  2. Every code has exactly three letters, so ordering the finished strings by character code is the same as ordering by FROM and then by TO.
  3. Check the number format carefully: exactly five digits after the decimal point with trailing zeros kept, so a rate of 2 must appear as 2.00000 and 0.0001 as 0.00010.

Exchange Rates for Every Currency Pair

mediumSquarepointSoftware Engineer

You are given a dictionary usd_values that maps currency codes to their value in US dollars: "SGD": 0.73 means one unit of SGD is worth 0.73 USD. Produce the exchange rate for every ordered pair of distinct currencies, including USD itself, as strings of the form "FROM/TO:RATE", where RATE is how many units of TO one unit of FROM buys. Return the strings in sorted order.

Rules

  • USD is always one of the currencies and is worth exactly 1 USD, whether or not it appears in usd_values. If it does appear, its value is 1.0 and it still counts as one currency.
  • Include one string for every ordered pair (FROM, TO) of distinct currencies: both A/B and B/A appear, and no currency is paired with itself.
  • RATE is usd_values[FROM] / usd_values[TO], with USD's value taken as 1.0, computed in double precision and written with exactly five digits after the decimal point, rounded to the nearest such value, exactly as Python's f"{rate:.5f}" writes it: at least one digit before the decimal point, trailing zeros kept, and no sign, exponent or thousands separator.
  • Sort the strings in ascending order by character code (Python's default string ordering). Because every code is three uppercase letters, this is the same as sorting by FROM and then by TO.
  • If USD is the only currency, return an empty list.

Language notes

  • The mapping arrives as a dict in Python, a plain object in JavaScript, a java.util.Map<String, Double> in Java and a std::unordered_map<std::string, double> in C++. Return a list (array) of strings.
  • Every value and rate is a floating-point number and nothing exceeds 2^31 - 1, so no 64-bit integer type is needed. Within these constraints JavaScript's rate.toFixed(5) and C++'s "%.5f" produce the same text as Python's f"{rate:.5f}"; in Java, format with java.util.Locale.ROOT so the decimal separator is always a period.

Constraints

  • 0 <= len(usd_values) <= 100
  • Every key is exactly three uppercase English letters A–Z, and keys are distinct.
  • Every value v satisfies 0.01 <= v <= 100 and has at most six digits after the decimal point. If "USD" is a key, its value is 1.0.
  • Every rate therefore lies between 0.0001 and 10000. No exact rate lies within 1e-9 of a number exactly halfway between two consecutive five-decimal values, so correct rounding never depends on floating-point error.
  • With m distinct currencies including USD (at most 101), the output has m * (m - 1) strings, at most 10,100.

Example 1

Input:  usd_values = {"SGD": 0.73}
Output: ["SGD/USD:0.73000", "USD/SGD:1.36986"]

USD is added as the second currency. One SGD buys 0.73 USD, written with five decimals. One USD buys 1 / 0.73 = 1.369863... SGD, which rounds to 1.36986.

Example 2

Input:  usd_values = {"EUR": 1.1, "GBP": 1.25, "USD": 1.0}
Output: ["EUR/GBP:0.88000", "EUR/USD:1.10000", "GBP/EUR:1.13636",
         "GBP/USD:1.25000", "USD/EUR:0.90909", "USD/GBP:0.80000"]

USD is already present and counts once, giving three currencies and six pairs. For example, GBP/EUR is 1.25 / 1.1 = 1.136363..., written as 1.13636, and USD/EUR is 1 / 1.1 = 0.909090..., written as 0.90909.

Examples

Example 1
Input
({'SGD': 0.73},)
Output
['SGD/USD:0.73000', 'USD/SGD:1.36986']
Notes
Source Example 1: one non-USD currency with USD implicit gives exactly two strings, one per direction; 1 / 0.73 = 1.369863... rounds to 1.36986.
Example 2
Input
({'EUR': 1.1, 'GBP': 1.25, 'USD': 1.0},)
Output
['EUR/GBP:0.88000', 'EUR/USD:1.10000', 'GBP/EUR:1.13636', 'GBP/USD:1.25000', 'USD/EUR:0.90909', 'USD/GBP:0.80000']
Notes
Source Example 2: USD given explicitly counts once, so three currencies give six strings with no USD/USD.

Constraints

  • •0 <= len(usd_values) <= 100
  • •Every key is exactly three uppercase English letters A–Z, and keys are distinct.
  • •Every value v satisfies 0.01 <= v <= 100 and has at most six digits after the decimal point. If "USD" is a key, its value is 1.0.
  • •Every rate therefore lies between 0.0001 and 10000. No exact rate lies within 1e-9 of a number exactly halfway between two consecutive five-decimal values, so correct rounding never depends on floating-point error.
  • •With m distinct currencies including USD (at most 101), the output has m * (m - 1) strings, at most 10,100.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def currency_pair_rates(usd_values: dict[str, float]) -> list[str]:
Sample call
currency_pair_rates({'SGD': 0.73})
Language
Case 1
Input
({'SGD': 0.73},)
Expected
['SGD/USD:0.73000', 'USD/SGD:1.36986']
Case 2
Input
({'EUR': 1.1, 'GBP': 1.25, 'USD': 1.0},)
Expected
['EUR/GBP:0.88000', 'EUR/USD:1.10000', 'GBP/EUR:1.13636', 'GBP/USD:1.25000', 'USD/EUR:0.90909', 'USD/GBP:0.80000']

Browse More Questions

Show the approach

Approach

Algorithm: build the full currency table first. Start from {USD: 1.0} and copy every entry of usd_values over it, so USD is present exactly once whether or not it was given (an explicit USD key has value 1.0, so overwriting changes nothing). Sort the m codes. Then for each FROM in sorted order and each TO in sorted order with TO != FROM, append FROM + '/' + TO + ':' followed by value[FROM] / value[TO] formatted with exactly five digits after the decimal point.

Ordering invariant: every code has exactly three characters, so two output strings first differ inside the FROM code or, when FROM is equal, inside the TO code; the RATE part is never reached because no (FROM, TO) pair repeats. Character-code order of the strings is therefore lexicographic order of (FROM, TO), which is exactly the order the nested loops over the sorted codes emit, and the loops emit every ordered pair of distinct currencies once, m * (m - 1) strings in total.

Rounding: the double quotient differs from the exact rate by a relative error of a few units of 2^-53 (an absolute error far below 1e-9 even at 10000), while the guarantee keeps every exact rate more than 1e-9 from a halfway point between five-decimal values. Rounding the double to five decimals therefore yields the same digits as rounding the exact rate, and Python, JavaScript, Java and C++ formatting agree.

Edge cases: an empty mapping, or one holding only USD, leaves a single currency and returns []; identical values give 1.00000 in both directions; integer-valued rates still print five decimals (2.00000); 0.999996 carries into the integer part as 1.00000 and 99.999999 prints as 100.00000; the extreme rates print as 0.00010 and 10000.00000.

Time complexity:
O(m^2)
Space complexity:
O(m^2)