Transitive Greater-Than Relation Queries
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Bloomberg OA reported in October 2019 looks like a simple comparison problem until a query lands on a pair your first idea can't handle. You get a pile of a > b facts and have to answer GREATER, LESS or UNKNOWN for each query. It's a directed graph reachability problem in disguise. With up to 10^5 relations and queries, the brute-force version dies fast. If you blank on the efficient approach mid-assessment, StealthCoder sits invisibly on your screen as a safety net and hands you a working path.
The problem
Each [a,b] in relations means a > b. The directed relation is acyclic. For each query [x,y], return GREATER if a transitive path proves x>y, LESS if a path proves y>x, and UNKNOWN otherwise. Function resolveRelations(relations: int[][], queries: int[][]) → String[] Examples Example 1 relations = [[1,2],[1,4],[4,7],[7,5]] queries = [[1,5],[5,1],[2,4]] return = ["GREATER","LESS","UNKNOWN"] 1 reaches 5 transitively; the reverse query is LESS; 2 and 4 are unrelated. Constraints At most 10^5 relations and queries.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Model each relation as an edge from a to b. A query [x,y] is GREATER if y is reachable from x, LESS if x is reachable from y, else UNKNOWN. The naive trap is running a fresh DFS or BFS per query. That's 10^5 queries times 10^5 edges, which times out. Also watch the edge case where x equals y, since the graph is acyclic and a node can't be greater than itself, so that should be UNKNOWN unless the statement says otherwise. Better options: cache reachability per source node with memoized DFS when queries repeat sources, or group queries by source and run one traversal per distinct source. Reachability sets over a DAG can get huge, so don't store full sets for every node. If you freeze on the bookkeeping, StealthCoder can supply the grouped-traversal structure during the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Transitive Greater-Than Relation Queries cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Transitive Greater-Than Relation Queries FAQ
How hard is the Bloomberg transitive relations question really?+
The idea is easy: reachability in a DAG. The difficulty is scale. With 10^5 relations and queries, a fresh search per query is too slow, so you need to reuse work. Most candidates who fail just wrote the naive per-query DFS.
What's the core trick?+
Turn a > b into a directed edge a to b, then answer each query with reachability in both directions. GREATER means x reaches y, LESS means y reaches x. Anything else is UNKNOWN. The speedup comes from batching or caching traversals.
What edge cases should I test?+
Test x equal to y, nodes that appear only in queries and not in relations, repeated queries, and long chains that could blow recursion depth. The acyclic guarantee means you don't need cycle detection, but deep chains can overflow a recursive DFS.
Should I use DFS or BFS?+
Either works for reachability. Iterative BFS or an explicit-stack DFS is safer because a chain of 10^5 nodes can overflow recursion in many languages. Pick whichever you can write without bugs under time pressure.
How do I prepare in 48 hours?+
Practice writing adjacency-list graph building and an iterative traversal until it's automatic. Then practice grouping queries by source node to avoid repeated work. Run your solution on a long chain and a wide fan-out to check both speed and recursion safety.