Answer Currency Conversion Queries from Known Exchange Ratios

Last updated: November 29, 2026

Quick Overview

Given equations like A / B = k between currency codes and a list of queries X / Y, return each query's value derived from the known ratios, or -1.0 if it cannot be determined.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given equations like A / B = k between currency codes and a list of queries X / Y, return each query's value derived from the known ratios, or -1.0 if it cannot be determined.

Framed around unit and currency conversion in Google Search, this question checks whether you model ratios as a weighted graph and pick between per-query DFS and weighted union-find. Interviewers often push toward union-find with path compression once you mention many queries.

What the Interviewer Expects
  • Models each equation as two directed weighted edges
  • Explains why multiplying along any path gives the same answer for consistent input
  • Implements weighted union-find (or DFS) correctly
  • Returns -1 for unknown symbols, including X / X with X unseen
  • Discusses floating-point accumulation
Key Topics to Cover
Union-Find
Weighted Graph
DFS
Path Compression
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if equations may conflict; how do you detect inconsistency?
  • Equations and queries interleave online; which approach still works?
  • How would you find the best rate when there are fees on each conversion (arbitrage detection)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: equations pairs, values ratios (positive floats), queries pairs.
  • Output: list of floats, -1.0 for undeterminable queries.
  • Input is assumed consistent. Number of queries may be large...
High-Level Design

Use weighted union-find. Each symbol has a parent and a weight meaning x / parent[x]. find(x) returns the root and the product of weights to it, compressing the path so later finds are O(alpha). F...


Submit Your Answer
Markdown supported