Rightward Bacteria Consumption
Reported by candidates from Cresta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Cresta reportedly put Rightward Bacteria Consumption in front of candidates in July 2026, and it looks easy until one detail trips you up. It's a left-to-right pass with a stack of survivors, and the whole problem lives in how you compare each new bacterium to only the nearest survivor. If the OA invite is sitting in your inbox, read the rules twice. Most wrong answers come from comparing against the wrong neighbor. StealthCoder runs invisibly as a safety net during the live OA if you blank, but the logic here is small enough to own before you start.
The problem
Bacteria stand from left to right. The string types gives each bacterium's type, and sizes[i] gives its fixed size. Process bacteria from left to right while maintaining the bacteria that have survived so far. When the next bacterium is processed, compare it only with the nearest surviving bacterium to its left. That left bacterium immediately consumes the new bacterium exactly when the left bacterium is larger and their types differ. Consumption does not change the eater's size. If the condition is false, the new bacterium also survives. Return the original zero-based indices of all surviving bacteria in left-to-right order. Function survivingBacteria(types: String, sizes: int[]) → int[] Examples Example 1 types = "ABCD" sizes = [9,4,7,2] return = [0] The bacterium at index 0 is larger than every later bacterium and has a different type from each, so it consumes indices 1, 2, and 3. Example 2 types = "ABCA" sizes = [5,7,3,2] return = [0,1] Index 0 cannot consume the larger bacterium at index 1. Index 1 then consumes indices 2 and 3, leaving indices 0 and 1. Example 3 types = "AABC" sizes = [10,1,2,1] return = [0,1,2] Index 0 and index 1 have the same type, so both survive. Index 1 is too small to consume index 2, which also survives and then consumes index 3. Constraints 1 <= types.length == sizes.length <= 10^5. Every character of types is A, B, C, or D. 1 <= sizes[i] <= 10^9. A bacterium's size never changes after consumption. Equal-size or equal-type neighboring survivors do not consume one another.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a stack of surviving indices. For each new index i, look at the top of the stack only. If the top is larger in size and has a different type, i gets eaten and you skip it. Otherwise push i. That's the full algorithm, O(n) time and O(n) space. The pitfall is the edge case that breaks a naive solution: a new bacterium that survives becomes the new nearest-left neighbor, and a bacterium that gets eaten never does. Don't compare against everything to the left, and don't pop anything. Eaters never grow, and survivors are never removed later, since only the new one gets checked. Example 3 shows it: index 1 survives next to index 0 because the types match, then index 2 survives because index 1 is too small. Use strict greater-than for size. If you freeze in the live OA, StealthCoder is the hedge that hands you this stack loop fast.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Rightward Bacteria Consumption 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 Cresta's OA.
Cresta 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.
Rightward Bacteria Consumption FAQ
How hard is Rightward Bacteria Consumption really?+
Easy to medium. It's a single pass with a stack, and the code is under fifteen lines. The difficulty is reading the rule correctly: only the nearest survivor to the left matters, and consumption needs both a larger size and a different type.
What's the trick to this problem?+
Use a stack of surviving indices and compare each new bacterium only to the top. If the top is strictly larger and the types differ, skip the new one. Otherwise push it. Nothing ever gets popped, so a plain list works as the stack.
What's the most common mistake?+
Comparing the new bacterium to every earlier one, or to index i-1 even when that one was eaten. The rule says nearest surviving bacterium. Another slip is using greater-or-equal instead of strictly greater for size.
Do I need to worry about the 10^5 constraint?+
Yes. With n up to 10^5, anything quadratic risks timing out. The single-pass stack approach is O(n), so you're safe. Sizes go up to 10^9, which fits in a normal 32-bit signed int, so overflow isn't a concern.
How do I prepare for this in 48 hours?+
Hand-trace the three examples until the survivor logic feels automatic. Then write the stack loop from memory and test the edge cases: all same type, equal sizes, a single bacterium, and a strictly decreasing size sequence with all types different.