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
- 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.
- 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.
- 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.