Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Given 'a/b = k' equations (e.g. a/b = 2, b/c = 3), answer queries like a/c. Which model and algorithm fit?

  1. A. A weighted directed graph (edge a→b with weight k and b→a with 1/k); answer each query by DFS/BFS multiplying weights, or with weighted union-find
  2. B. A min-heap of ratios
  3. C. Sorting the variables alphabetically
  4. D. Dynamic programming over subsets of variables

Correct answer: A. A weighted directed graph (edge a→b with weight k and b→a with 1/k); answer each query by DFS/BFS multiplying weights, or with weighted union-find

Each equation is an edge; a/c is the product of weights along any path a → c (2 × 3 = 6), and −1 when disconnected. Weighted union-find stores each node's ratio to its root for near O(1) queries.

More Coding Interview Problems questions

Exams that ask this topic