Minimum Operations with Multiply by Two and Divide by Three
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Bloomberg OA, reported April 2021, is the string "****/*" that gets you from 1 to 10 without ever leaving the range 1 to maxState. Start at 1, double or floor-divide by three, return the shortest operation string, break ties with * before /. It looks like a puzzle but it's a graph search in disguise. If you blank on the tie-break part during the live assessment, StealthCoder runs invisibly and can hand you the structure. Read this first and you probably won't need it.
The problem
Start at integer 1. Operation * replaces x with 2x; operation / replaces x with floor(x/3). Every intermediate state must remain between 1 and maxState. Return a shortest operation string reaching target. Break shortest ties lexicographically with * before /. Return IMPOSSIBLE when unreachable. Function shortestOperationSequence(target: int, maxState: int) → String Examples Example 1 target = 10 maxState = 16 return = "****/*" 1->2->4->8->16->5->10. Example 2 target = 3 maxState = 16 return = "****/*/" The source's example reaches 3 through 16,5,10,3. Constraints 1 <= target <= maxState <= 10^6.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Treat each integer from 1 to maxState as a node with at most two outgoing edges: x to 2x and x to floor(x/3), both only if the result stays within 1 and maxState. That's at most 10^6 nodes, so BFS from 1 is fine. The trick is the lexicographic tie-break. Expand the * edge before the / edge in every BFS layer, and the first time you reach a node is the lexicographically smallest shortest path, since the queue preserves order of discovery. Store a parent pointer and the operation character per node, then walk back from target and reverse. Pitfalls: forgetting the lower bound of 1, letting 2x overflow past maxState, and building full strings in the queue, which blows up memory. Return IMPOSSIBLE if target is never visited. StealthCoder is the hedge if the parent-pointer reconstruction slips your mind mid-OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Minimum Operations with Multiply by Two and Divide by Three 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Operations with Multiply by Two and Divide by Three FAQ
What's the core trick in this Bloomberg problem?+
Model it as a shortest path on integers 1 to maxState and run BFS from 1. Each state has two edges, multiply by two and floor-divide by three. BFS gives the minimum number of operations, and the order you expand edges handles the tie-break.
How do I get the lexicographic tie-break right?+
Always try * before / when expanding a node, and mark nodes visited on first discovery. Because BFS processes nodes in discovery order within each layer, the first path found to any node is the lexicographically smallest among the shortest ones. No extra comparison needed.
Should I store whole strings in the BFS queue?+
No. With up to 10^6 states, copying strings costs too much memory and time. Keep a parent array and an operation-character array, then rebuild the answer by walking back from target to 1 and reversing the result.
What cases return IMPOSSIBLE?+
Any target BFS never reaches within the bounds. Remember every intermediate value must stay between 1 and maxState, so a doubling that exceeds maxState is illegal. If target is never marked visited when the queue empties, return IMPOSSIBLE.
How do I prepare for this in 48 hours?+
Write BFS with parent pointers on an implicit graph a couple of times, then trace the 10 and maxState 16 example by hand. Test target equal to 1, which should give an empty string, and an unreachable target. That covers most of the traps in this one.