Currency converter
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-06-01
Problem Statement
Design a Currency Converter System that allows users to convert an amount from one currency to another in real-time using exchange rates. The system should support multiple currencies, accurate conversion, caching, and extensibility for new features.
Functional Requirements (FR)
Currency Conversion
•Convert amount from a source currency to a target currency.
•Use up-to-date exchange rates.
Exchange Rate Management
•Fetch exchange rates from an external API (e.g., Forex provider).
•Support manual overrides for rates (admin feature).
•Cache frequently used exchange rates.
User Interaction
•User inputs: amount, source currency, target currency.
•System outputs: converted amount, rate used, timestamp.
History & Tracking (Optional)
•Maintain conversion history for a user.
•Show recent conversion results.
Non-Functional Requirements (NFR)
•Accuracy – Support floating-point precision for financial data.
•Scalability – Handle thousands of concurrent conversion requests.
•Low Latency – Conversion should be near real-time.
•Extensibility – Easy to add new currencies or rate providers.
•Resilience – Handle API failures with fallback (e.g., last cached rate).
Core Entities & Attributes
•Currency → code (USD, INR, EUR), name, symbol
•ExchangeRate → fromCurrency, toCurrency, rate, timestamp
•ConversionRequest → amount, fromCurrency, toCurrency
•ConversionResult → convertedAmount, rateUsed, timestamp
•RateProvider (interface) → getRate(from, to)
•CacheManager → storeRate(rate), getRate(from, to)
•CurrencyConverter (Facade) → convert(request)
Key Operations / APIs
•convert(amount, fromCurrency, toCurrency) → returns converted amount.
•getExchangeRate(fromCurrency, toCurrency) → fetch live or cached rate.
•updateRate(fromCurrency, toCurrency, newRate) (Admin).
•getConversionHistory(userId) (Optional).
Design Considerations
Data Precision
•Use BigDecimal (Java) or Decimal (Python) to avoid floating-point errors.
Rate Fetching & Caching
•Cache rates for commonly used currency pairs.
•Define TTL (time-to-live) for cached rates.
Concurrency
•Ensure thread-safe updates to rate cache.
Design Patterns
•Strategy Pattern → Different rate providers (API, DB, manual).
•Factory Method → Create Currency or ConversionRequest.
•Decorator → Add logging, rate-limiting, or auditing to conversions.
Example
Input:
Convert 100 USD → INR
Exchange Rate:
1 USD = 83.20 INR
Output:
100 USD = 8320 INR (Rate: 83.20, Timestamp: 2025-09-21 18:00)
✅ This problem tests:
•OOP design for services + entities.
•Precision handling in financial applications.
•Use of caching, external API integration, and extensibility.