Design a Currency Conversion Service
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-10-01
Problem Statement
Design a service that provides currency conversion between different currencies in real-time. The focus is on object-oriented design, modularity, accurate conversion, and handling external data sources.
✅ Functional Requirements
1.Currency Conversion
•Convert an amount from one currency to another using the latest exchange rates.
•Support multiple currency pairs (e.g., USD → INR, EUR → USD).
2.Exchange Rate Management
•Fetch and update exchange rates from reliable external APIs or internal feeds.
•Maintain historical rates for auditing or past conversions.
•Support caching of rates to reduce latency and API calls.
3.API Interface
•Expose methods or endpoints to:
•Convert an amount (convert(fromCurrency, toCurrency, amount))
•Retrieve current exchange rates (getRate(fromCurrency, toCurrency))
•Retrieve historical rates (getHistoricalRate(date, fromCurrency, toCurrency))
4.Optional Features
•Support batch conversions.
•Handle multiple locales and formatting of amounts.
•Rate alerts when exchange rate crosses a threshold.
🔐 Constraints & Assumptions
•Assume currency codes follow ISO 4217 standard (USD, INR, EUR, etc.).
•Rates are updated periodically (e.g., every 1 hour).
•Service should handle high concurrency efficiently.
•Focus is on LLD and modular system design, not front-end UI.
🧱 Key Components & Classes
1.Currency Class
1
2class Currency {
3private:
4 string code; // e.g., USD, INR
5 string name; // e.g., US Dollar, Indian Rupee
6public:
7 string getCode();
8 string getName();
9};1.ExchangeRate Class
1
2class ExchangeRate {
3private:
4 Currency* fromCurrency;
5 Currency* toCurrency;
6 double rate;
7 DateTime timestamp;
8public:
9 double getRate();
10};1.CurrencyConverter Class
1
2class CurrencyConverter {
3private:
4 map<pair<string, string>, ExchangeRate*> ratesCache;
5 ExchangeRateProvider* rateProvider;
6public:
7 double convert(string fromCurrency, string toCurrency, double amount);
8 double getRate(string fromCurrency, string toCurrency);
9 void updateRates();
10};1.ExchangeRateProvider Class
1
2class ExchangeRateProvider {
3public:
4 ExchangeRate* fetchRate(string fromCurrency, string toCurrency);
5 vector<ExchangeRate*> fetchAllRates();
6};1.Cache / History Management (Optional)
•Maintain historical exchange rates for auditing or reporting.
•Optional: LRU cache or in-memory store for frequently accessed rates.
🔄 Example Workflow
1.User requests conversion: convert("USD", "INR", 100).
2.CurrencyConverter checks cache → if outdated, fetches new rate from ExchangeRateProvider.
3.Conversion is calculated → amount * rate.
4.Response is returned with amount and rate used.
5.Logs are maintained for auditing historical rates if required.
🧪 Expected Deliverables
•Class diagram showing Currency, ExchangeRate, CurrencyConverter, and ExchangeRateProvider.
•Implementation of conversion, caching, rate updates, and optional historical tracking.
•Test cases:
•Convert between different currencies
•Verify caching works and rates update periodically
•Handle missing or invalid currency codes
•Batch conversion (optional)
💡 Discussion Points
•Handling high concurrency and frequent rate updates.
•Trade-offs between real-time API calls vs cached rates.
•Optional: supporting historical conversion for past dates.
•Extensibility to support cryptocurrency conversion in the future.