Reported May 2020
Bloombergdesign

Trending Stock

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

Every get in this Bloomberg Trending Stock problem is guaranteed valid, so you never handle an empty case and can spend all your attention on the ordering rule. Reported in May 2020, it's a frequency stack in disguise. Process pushes a ticker. Get pops the ticker with the highest count, and the newest occurrence wins ties. With up to 10^4 operations, a rescan per get might squeak by, but the intended answer is O(1) per operation. If you recognize the shape, you're done fast. If you blank, StealthCoder runs invisibly during the live OA and gives you the structure, so you're not staring at a blinking cursor.

The problem

Process commands:
process: record one occurrence of stocks[i], return null.
get: choose the stock with greatest current occurrence count, breaking ties by the most recent remaining occurrence; return it and remove that most recent occurrence.
Every get is valid. Return one string per operation.

Function
runTrendingStock(operations: String[], stocks: String[]) → String[]

Examples
Example 1
operations = ["process","process","process","get","get","process","get"]
stocks = ["TSLA","AAPL","TSLA","","","NFLX",""]
return = ["null","null","null","TSLA","AAPL","null","NFLX"]
TSLA first leads 2-1; after one removal the tie favors AAPL's later remaining occurrence, then NFLX is newest.

Constraints
At most 10^4 operations.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two maps. Keep count[stock] for the current frequency, and groups[freq] as a stack of stocks that reached that frequency. On process, increment the count and push the stock onto groups[newCount]. Track maxFreq. On get, pop from groups[maxFreq], decrement that stock's count, and if groups[maxFreq] is now empty, decrement maxFreq. That handles the tie rule for free, because each group is ordered by when the stock hit that level. The common pitfall is storing only the latest index per stock. After a removal, the previous occurrence matters, and that breaks. Another trap is forgetting to emit the string "null" for process operations. Check it against the example: TSLA, AAPL, TSLA, then get gives TSLA, and the tie falls to AAPL. If the live OA freezes your memory of this, StealthCoder is the hedge that keeps you moving.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Trending Stock 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum frequency stack. If you have time before the OA, drill that.

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Trending Stock FAQ

What's the trick in Bloomberg's Trending Stock problem?+

Treat it as a maximum frequency stack. Keep a count map plus a map from frequency to a stack of stocks. Each process pushes onto the stack for the new count. Each get pops from the stack at the max frequency. That gives O(1) per operation and handles the recency tiebreak automatically.

Do I need a heap for this?+

You can use one with a counter as a timestamp, ordering by count then recency. But removing the most recent occurrence means stale entries and lazy deletion, which gets messy. The frequency-to-stack approach is simpler, faster, and has fewer ways to break under pressure.

How hard is this really?+

Medium. The statement looks wordy, but the logic is small once you see the frequency stack. Most failures come from misreading the tiebreak or botching the max frequency update after a pop. Trace the sample by hand first and the code almost writes itself.

What should get return for process operations?+

The string "null" at that position. The function returns one string per operation, so the output array has the same length as operations. The stocks entry for a get is an empty string and should be ignored. Mixing those up is an easy way to fail hidden tests.

How do I prepare for this in 48 hours?+

Write the two-map solution from scratch once, then run the sample by hand. Test edge cases: one stock repeated, all distinct stocks, and a get that drops maxFreq. Then do one or two other design-style problems that need O(1) operations. Two focused hours beats a cram session.

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