Partition Labels
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in November 2023, and the whole solution hangs on a tiny hash map: the last index where each letter appears. If you're taking the OA soon, that's the entire game. Partition Labels looks like a string-slicing puzzle, but it's really a greedy sweep with a lookup table. Once you see it, the code is about ten lines. The trap is overthinking it because the hinted tag says dynamic programming. You don't need DP here. StealthCoder sits invisibly on your screen as a safety net if you blank on the live OA, but this one is easy enough to carry in your head.
The problem
Split text into as many nonempty contiguous parts as possible so each distinct character appears in at most one part. Return the part lengths from left to right. Function partitionLabels(text: String) → int[] Examples Example 1 text = "abcabcabdefffedgijhkij" return = [8,7,1,6] The maximal parts are abcabcab, defffed, g, and ijhkij. Constraints 0 <= text.length <= 10^5. The text contains lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a map from each character to its last index in the text. Then sweep left to right while tracking two things: the start of the current part and the farthest last-occurrence seen so far (call it end). For each index i, update end to the max of end and last[text[i]]. When i equals end, every character in the current part has all its occurrences inside it, so cut here, push end - start + 1, and set start to i + 1. That's O(n) time and O(1) space since the alphabet is 26 letters. Common pitfalls: forgetting to take the max, cutting on the first character's last index only, and mishandling the empty string, which should return an empty list. Don't reach for DP or interval merging unless you want extra code. If your mind goes blank mid-assessment, StealthCoder can surface this greedy sweep on screen without the proctor seeing it.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Partition Labels 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as partition labels. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Partition Labels FAQ
How hard is Partition Labels really?+
It's a medium on paper but easy once you know the trick. The solution is one pass with a last-index map. Most people struggle only because they try to simulate merging intervals or build a DP table, which adds complexity the problem doesn't need.
What's the trick for the Bloomberg version?+
Record the last index of every character first. Then scan, keep a running maximum of those last indexes, and close a part whenever your current index equals that maximum. That guarantees no letter shows up in two parts.
Is this a DP problem like the tag suggests?+
No. Despite the dynamic-programming hint, the clean answer is greedy with a hash map or 26-slot array. There are no overlapping subproblems to cache. Treat each character as an interval from first to last occurrence and extend greedily.
What edge cases should I test?+
Test the empty string, which should return an empty array. Test a single character, a string with all the same letter (one part of full length), and all distinct letters (every part has length 1). Also run the example to confirm [8,7,1,6].
How do I prepare in 48 hours?+
Write this solution from scratch twice without looking. Then do two or three related greedy-with-last-index or interval problems so the pattern sticks. Practice stating time and space complexity out loud: O(n) time, O(1) space for 26 lowercase letters.