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?
- 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
- B. A min-heap of ratios
- C. Sorting the variables alphabetically
- 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
- Problem: Evaluate division queries on the ratio graph a/b = 2, b/c = 3, d/e = 4. python from collections import defaultdict g =…
- Problem: Reverse a word after prepending a marker using Java's StringBuilder. java StringBuilder sb = new StringBuilder("interview")…
- Problem: Find the longest substring in which every character appears at least k times (divide and conquer on characters that are too…
- Problem: Return the minimum number of characters to insert to make a string a palindrome. python def mininsert(s): t = s[::-1]; n = len(s)…
- Problem: Count the number of islands after each 'add land' operation on an initially all-water grid. python def islandsonline(R, C, ops)…
- Problem: Return the length of the shortest path that visits every node of a small undirected graph (start and end anywhere, edges…