Convert Between Currencies Using Only Known Exchange Rates

Last updated: November 29, 2026

Quick Overview

Given known exchange rates as (A, B, rate) meaning 1 A equals rate B, answer queries (X, Y) with the implied rate from X to Y, or -1.0 if it cannot be derived.

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

0

0

0 solved


Given known exchange rates as (A, B, rate) meaning 1 A equals rate B, answer queries (X, Y) with the implied rate from X to Y, or -1.0 if it cannot be derived.

A weighted graph traversal in the style of Meta's onsite coding interviews, framed around Meta Pay and ads billing converting advertiser budgets across currencies. The interviewer checks that you add reverse edges with reciprocal weights and multiply rates along a search path.

What the Interviewer Expects
  • Builds a graph with edge A to B of rate r and B to A of 1/r
  • Answers each query by DFS or BFS, multiplying along the path
  • Returns -1.0 for unknown currencies or disconnected pairs
  • Returns 1.0 for X to X when X is known
  • Mentions weighted union-find for many queries
Key Topics to Cover
Graphs
DFS
Weighted edges
Union-find
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
  • How would you speed up many queries on a static rate table?
  • What if rates are inconsistent around a cycle, and how could you detect arbitrage?
  • How do you limit floating point error on long chains?
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: rates as (a, b, r) with r > 0 and queries as (x, y). Output: list of floats. Unknown currency gives -1.0. Assume the given rates are consistent. Up to a few hundred currencies and queri...

High-Level Design

Treat currencies as nodes. For each rate, add a -> b with weight r and b -> a with weight 1/r. A conversion from x to y is the product of weights along any path, which is the same along every path...


Submit Your Answer
Markdown supported