Microorganism Eating Simulation: Largest Survivor After Left-to-Right Rounds
OpenAISoftware Engineer · Online AssessmentLast updated
Simulate a row of microorganisms in which each one may eat a strictly smaller neighbor from a different family, with rounds resolved from left to right, and report the family and size of the largest survivor at equilibrium. It tests precise rule-following, list mutation during iteration, and careful per-round bookkeeping.
Several microorganisms are lined up in a row. Each one belongs to a family, identified by a letter, and has a size, a positive integer. A microorganism can eat a neighbor directly to its left or right if that neighbor belongs to a different family and is strictly smaller. The simulation runs in rounds until no microorganism can eat another. When it stops, return the largest microorganism as its family letter, one space and its size.
Function Signature
def largest_at_equilibrium(organisms: list[tuple[str, int]]) -> str:
organisms[i] is (family, size) for the i-th microorganism from the left.
Rules
- In each round, the microorganisms take turns from left to right. After a turn, the next turn belongs to the microorganism immediately to the right of the one that just took its turn, in the row as it is at that moment. A microorganism that is eaten before its turn gets no turn.
- On its turn, a microorganism checks its left neighbor first and then its right neighbor, and eats the first one that is a valid target. If neither is a valid target, it does nothing.
- A neighbor is a valid target if it belongs to a different family, its current size is strictly smaller than the eater's current size, and it has not eaten during the current round.
- When a microorganism eats, its size grows by the size of the target, and the target is removed, so the microorganisms on either side of the gap become neighbors. These changes take effect at once, so later turns in the same round see the new sizes and neighbors.
- A microorganism eats at most once per round, on its own turn, and a microorganism that has eaten during a round cannot be eaten during that round. Because turns run from left to right, when a microorganism could be eaten by either neighbor, the left neighbor's turn comes first.
- The simulation stops after the first round in which nothing is eaten.
- Return the family letter, a single space and the size of the largest remaining microorganism, for example
"B 15".
Constraints
1 <= len(organisms) <= 20- Each family is a single uppercase English letter from
"A"to"Z". 1 <= size <= 10^6for every microorganism.- The input guarantees that, when the simulation stops, exactly one microorganism has the maximum size.
Examples
Example 1
Input: organisms = [("A", 2), ("B", 5), ("C", 3), ("C", 4), ("A", 1)]
Output: "B 15"
Round 1: A 2 cannot eat B 5. B 5 checks its left neighbor first and eats A 2, becoming B 7. C 3 cannot eat B 7, and its right neighbor C 4 is the same family. C 4 eats A 1 and becomes C 5. The row is now B 7, C 3, C 5. Round 2: B 7 eats C 3 and becomes B 10; C 5 cannot eat B 10. Round 3: B 10 eats C 5 and becomes B 15. Round 4: nothing is eaten, so the simulation stops.
Example 2
Input: organisms = [("A", 1), ("B", 2), ("C", 10)]
Output: "C 13"
Round 1: A 1 cannot eat B 2. B 2 eats A 1 and becomes B 3. On its turn, C 10 cannot eat B 3, because B 3 has eaten during this round. Round 2: C 10 eats B 3 and becomes C 13. Round 3: nothing is eaten.
Example 3
Input: organisms = [("A", 5), ("A", 2), ("B", 2), ("B", 6)]
Output: "B 6"
Each pair of neighbors is either the same family or the same size, so nothing is eaten in round 1 and the largest microorganism is B 6.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.