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