Reported May 2018
Bloomberghash table

Group Values into Equivalence Classes

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Bloomberg reported this one in May 2018, and the title sounds fancier than the task. Strip the wording and it's grouping: given a matrix that tells you which values are equivalent, bucket them and return the buckets in first-appearance order. If your OA invite is sitting in your inbox, this is a hash-table-flavored problem you can finish in a handful of lines once you see it. The matrix is a guarantee, not a puzzle. And if you blank on the live OA, StealthCoder runs invisibly on your desktop and can hand you the approach in real time.

The problem

equivalent[i][j] is the result of an equivalence callback for values[i] and values[j]. The matrix describes an equivalence relation.
Return the equivalence classes in the order their first member appears, preserving input order inside each class.

Function
equivalenceClasses(values: int[], equivalent: int[][]) → int[][]

Examples
Example 1
values = [1,2,3,4,5,6]
equivalent = [[1,0,0,1,0,0],[0,1,0,0,1,0],[0,0,1,0,0,1],[1,0,0,1,0,0],[0,1,0,0,1,0],[0,0,1,0,0,1]]
return = [[1,4],[2,5],[3,6]]
The matrix encodes equality modulo three.

Constraints
values.length == equivalent.length.
The matrix is square, reflexive, symmetric, and transitive.
values.length <= 1000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that the relation is already reflexive, symmetric and transitive, so you never need union-find or graph traversal. Walk i from 0 to n-1. If i is already assigned to a class, skip it. Otherwise it's the first member of a new class, so scan row i of the matrix, and every j where equivalent[i][j] is 1 joins that class in increasing j order. That gives first-appearance ordering and input order inside each class for free. Cost is O(n^2), fine for n up to 1000. The common pitfall is overbuilding: DFS, union-find, or sorting by a key you don't have. Another is forgetting the visited check and emitting duplicate classes. Also return the values, not the indices. If you freeze mid-assessment, StealthCoder is the safety net that reads the problem and gives you this scan.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Group Values into Equivalence Classes 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Group Values into Equivalence Classes FAQ

How hard is the Bloomberg equivalence classes problem really?+

Easy to medium. The matrix guarantees a valid equivalence relation, so a visited array and a row scan solve it. The difficulty is resisting the urge to build a graph or union-find structure. If you see the guarantee, it's about ten lines.

What's the trick to solve it fast?+

Iterate indices in order. If index i isn't assigned yet, it starts a new class. Scan row i, collect every j where the entry is 1, and mark them assigned. Ordering comes out right automatically because both loops go left to right.

Do I need union-find or DFS here?+

No. Transitivity means row i already lists the whole class of i. Union-find works but adds code and risk for no gain. A single pass with a visited array gives the same answer in O(n^2) time, which fits n up to 1000.

What mistakes break the output?+

Three common ones. Not skipping already-assigned indices, which duplicates classes. Returning indices instead of values. Building classes through a hash map without keeping first-appearance order, which scrambles the output order the problem demands.

How do I prepare for this in 48 hours?+

Practice grouping problems where you bucket items and preserve order. Write this one from scratch twice, then test edge cases: one element, all equivalent, none equivalent beyond themselves. Know the O(n^2) cost and why it's acceptable at n up to 1000.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it