KatInterview

Cell Absorption Simulation: Dominant Cell After Feeding Rounds

Arrays & HashingAuto-graded · 28 test cases
HardAuto-gradedArrays & Hashingsimulationarrayslist-mutation

Last updated

Simulate a row of cells that absorb weaker neighbors across repeated left-to-right rounds and identify the largest survivor at equilibrium.

Cell Absorption Simulation

A row of cells is placed side by side. Each cell belongs to a species (a single uppercase letter) and has a strength (a positive integer). Cells feed on their immediate neighbors according to the rules below. The feeding process continues in rounds until no cell can absorb another. At that point, return the species and strength of the strongest remaining cell.

Rules

Valid target. A neighbor qualifies as a valid target if all three conditions hold:

  • It belongs to a different species than the attacker.
  • Its current strength is strictly less than the attacker's current strength.
  • It has not absorbed another cell during the current round.

Turn order within a round. Cells take turns from left to right, using the row as it stands at that exact moment. A cell that is absorbed before its own turn never takes one.

What happens on a turn. The active cell first checks its left neighbor. If that neighbor is a valid target, the cell absorbs it: the attacker's strength increases by the target's strength, and the target is removed from the row (closing the gap instantly). If the left neighbor does not qualify, the cell checks its right neighbor under the same conditions. If neither neighbor qualifies, the cell does nothing.

Immediate effect. Every absorption takes effect at once. Cells that act later in the same round see the updated strengths and updated adjacency.

Per-round limits. Each cell may absorb at most once per round. A cell that has absorbed during the current round cannot itself be absorbed for the remainder of that round.

Stopping condition. The simulation ends after the first round in which no absorption occurs.

Result. Return a string of the form "<species> <strength>", for example "B 15". The input guarantees that exactly one cell has the maximum strength when the simulation ends.

Constraints

  • 1 <= len(cells) <= 20
  • Each species is a single uppercase English letter ("A" to "Z").
  • 1 <= strength <= 10^6 for every cell.
  • When the simulation ends, exactly one cell has the maximum strength.

Examples

Example 1

Input: dominant_cell_after_feeding([['A', 2], ['B', 5], ['C', 3], ['C', 4], ['A', 1]])

Output: 'B 15'

Example 2

Input: dominant_cell_after_feeding([['A', 1], ['B', 2], ['C', 10]])

Output: 'C 13'

Example 3

Input: dominant_cell_after_feeding([['A', 5], ['A', 2], ['B', 2], ['B', 6]])

Output: 'B 6'

Hints

Hint 1

Store each cell as a mutable object (e.g. a three-element list) so that strength updates and removals during a round are immediately visible to all later turns in the same round.

Hint 2

Walk the row with an explicit integer index rather than a for-each iterator. When a cell absorbs its left neighbor, that neighbor's slot is deleted and the absorber shifts one index to the left — do not increment the index in that case, just continue to the next iteration so the pointer lands on the absorber's right neighbor.

Hint 3

Track a boolean flag per cell indicating whether it has already absorbed something this round. Reset all flags at the start of each new round, and check the flag both to enforce the one-absorption limit and to grant immunity to cells that already acted.