Lottery with Add and Pick Operations
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure is the whole question here. Bloomberg reported this one in April 2021: a lottery with add and pick operations, run over three parallel arrays. Add appends a unique user, pick returns the user at a given index without removing anyone. It looks like a design problem, but it's really a test of whether you reach for a plain dynamic array and stop overthinking. If you're taking this OA in the next couple of days, the pattern is simple and the traps are small. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but you probably won't need it.
The problem
Process parallel arrays: add: append the unique users[i]; return null. pick: return the user at zero-based index choices[i] in insertion order. The supplied index represents a uniform random draw; picking does not remove the user. For add, the matching choice and unused user fields may be ignored. Function runLottery(operations: String[], users: String[], choices: int[]) → String[] Examples Example 1 operations = ["add","add","pick","add","pick"] users = ["Ada","Bob","","Cy",""] choices = [0,0,1,0,2] return = ["null","null","Bob","null","Cy"] The injected choices select Bob then Cy. Constraints Every choice for a pick is valid. Added users are unique.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that insertion order plus index lookup is exactly what an array or list gives you. Add is an append, pick is list[choices[i]]. Both are O(1), so the whole run is O(n). No hash map, no heap, no real randomness, because the choice index is already supplied. The pitfalls are all about output format. Add must emit the string "null", not a real null, and the result array must line up one to one with operations. Don't remove the picked user. Don't sort or dedupe, since users are already unique. Don't read choices on add, and don't read users on pick. Loop with a single index i, branch on operations[i], and push to the result. If you freeze on the signature or the "null" string, StealthCoder can read the problem on screen and hand you the loop live, invisible to the proctor.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Lottery with Add and Pick Operations 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 StealthCoderRelated leaked OAs
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.
Lottery with Add and Pick Operations FAQ
How hard is the Bloomberg lottery add and pick problem really?+
Easy. It's a simulation with a dynamic array. Add appends, pick indexes. Most of the difficulty is reading the parallel-array input format correctly and returning "null" as a string for add operations. If you code the loop cleanly, you're done in minutes.
What's the trick to this problem?+
There's no real trick beyond picking the right structure. A list preserves insertion order and gives O(1) index access, which is exactly what pick needs. The random draw is already injected through choices, so you never generate randomness yourself.
Does pick remove the user from the lottery?+
No. The problem says picking does not remove the user. The same person can be picked again later. Keep the list unchanged on pick and just return the element at choices[i].
What should add operations return in the output array?+
The string "null", per the example output. Every operation produces one entry, so the result has the same length as operations. Add gives "null", pick gives the username at the supplied index.
How do I prepare for this in 48 hours?+
Write the solution once from scratch in your language. Then practice similar design-style problems that combine a list with index or hash lookups. Check your handling of output types and parallel arrays, since that's where small mistakes happen on an easy question like this.