IP Prefix Store
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on Bloomberg's IP Prefix Store is counting covered addresses by adding up 2^(width - len) for every prefix. Overlaps double count, and your final number comes out wrong even when every true/false answer is right. This one was reported in October 2023, and it's a trie problem dressed up as networking. Insert the prefixes, answer each query by walking the bits, then count the union once. If you blank during the live OA, StealthCoder is the safety net that runs invisibly and gives you the structure while you keep typing.
The problem
Addresses are binary strings of exactly width bits. Each string in prefixes covers every address starting with it. Add all prefixes, then for each full-width query return lowercase true or false for whether any stored prefix covers it. Append one final decimal string equal to the number of distinct full-width addresses covered by the union of all prefixes. Function runIpPrefixStore(prefixes: String[], queries: String[], width: int) → String[] Examples Example 1 prefixes = ["1111","11"] queries = ["1100","1010","1111"] width = 4 return = ["true","false","true","4"] Prefix 11 covers 1100,1101,1110,1111; adding 1111 does not double count. Constraints 1 <= width <= 60. Prefixes contain only 0/1 and have length at most width. Queries have length exactly width.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a binary trie. Insert each prefix and mark the last node as terminal. If you hit a terminal node while inserting, the new prefix is already covered, so stop. For a query, walk its bits and return true the moment you pass a terminal node. Counting the union is the trap. Do a DFS from the root. When you reach a terminal node at depth d, add 2^(width - d) and don't descend further. That handles nesting automatically, so 1111 under 11 contributes nothing extra. Width goes up to 60, so the count can reach 2^60. Use a 64-bit long, and don't use floating point or Math.pow. Use a shift. Return the count as a decimal string appended last. Lowercase true and false, exactly. Watch the empty prefix too, since length 0 covers everything. StealthCoder is the hedge if the trie code slips under pressure in the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill IP Prefix Store 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
IP Prefix Store FAQ
What's the trick in the Bloomberg IP Prefix Store problem?+
Use a binary trie with terminal markers. Queries succeed if any terminal node appears along the path. For the final count, DFS and stop at the first terminal node on each branch, adding 2^(width - depth). Stopping early is what prevents double counting nested prefixes.
Why can't I just sum 2^(width - length) per prefix?+
Prefixes overlap. In the example, 11 already covers 1111, so adding both gives 5 instead of 4. You need the union. A trie with early stopping, or sorting and merging ranges, gives the correct distinct count.
Will the count overflow?+
Width can be 60, so the max is 2^60. That fits in a signed 64-bit integer. Compute it with a left shift on a long, not a double. Then convert to a string for the final array element.
How hard is this really?+
Medium. The trie is standard, but the union count and edge cases like the empty prefix or duplicate prefixes catch people. If you've written a trie before, it's maybe 30 lines. The risk is sloppy details, not the idea.
How do I prepare in 48 hours?+
Write a trie from scratch twice, once with insert and search, once with a DFS count over terminal nodes. Test with nested and duplicate prefixes. Check the output format: lowercase strings, with the count appended last.