Simulate a Circular Monster Battle and Return the Survivors' Health

Quick Overview

Simulate a battle where monsters in a circle each attack the next survivor every round, with damage and removals applied at the same time, and return the survivors' health in their original order. It tests careful simulation, circular indexing and handling simultaneous updates.

Simulate a Circular Monster Battle and Return the Survivors' Health

Company: Confluent

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of monsters standing in a line, each with a health value and an attack power. Simulate a battle between them for `n` rounds. In each round, every surviving monster (processed from left to right) attacks the next surviving monster to its right in a circular fashion. The last surviving monster attacks the first surviving monster. All attacks in a round are applied simultaneously. A monster dies when its health drops to `0` or below, and is removed from the battle at the end of the round. If only one monster (or none) remains, the battle ends early. Return the list of health values of all surviving monsters after `n` rounds, in their original left-to-right order. ### Function Signature ```python def monsters_battle(health: list[int], attack: list[int], n: int) -> list[int]: ``` ### Rules - Monster `i` starts with health `health[i]` and has attack power `attack[i]`. - At the start of a round, number the `m` survivors `0` to `m - 1` in their original order. Survivor `j` deals damage equal to its attack power to survivor `(j + 1) % m`, so with two survivors each one hits the other. - Every survivor attacks in the round, including one that dies in that same round. All damage is subtracted first, and only then are monsters with health `<= 0` removed. - If one or zero monsters remain at the start of a round, the battle stops. A lone monster never attacks itself. - Return the survivors' current health in original order. If every monster dies, return an empty list. ### Constraints - `1 <= health.length <= 1000` - `health.length == attack.length` - `1 <= health[i] <= 10000` - `1 <= attack[i] <= 100` - `1 <= n <= 10000` ### Examples ```text Input: health = [100,50,80], attack = [20,30,10], n = 3 Output: [70] ``` Each round, monster 0 hits monster 1 for 20, monster 1 hits monster 2 for 30, and monster 2 hits monster 0 for 10. Health goes `[90,30,50]`, then `[80,10,20]`, then `[70,-10,-10]`, so monsters 1 and 2 die after round 3 and only 70 is left. ```text Input: health = [200,200,200,200], attack = [10,10,10,10], n = 2 Output: [180,180,180,180] ``` Every monster takes 10 damage per round from its left neighbor, giving `[190,190,190,190]` after round 1 and `[180,180,180,180]` after round 2. All survive. ```text Input: health = [10,30,30], attack = [5,5,10], n = 3 Output: [5,15] ``` Round 1 leaves `[0,25,25]`, so monster 0 dies. From round 2 on, monsters 1 and 2 hit each other for 5 and 10 per round, so after two more rounds monster 1 has 25 - 20 = 5 and monster 2 has 25 - 10 = 15.

Overview: Simulate a battle where monsters in a circle each attack the next survivor every round, with damage and removals applied at the same time, and return the survivors' health in their original order. It tests careful simulation, circular indexing and handling simultaneous updates.

Confluent logo
Confluent
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of monsters standing in a line, each with a health value and an attack power. Simulate a battle between them for n rounds.

In each round, every surviving monster (processed from left to right) attacks the next surviving monster to its right in a circular fashion. The last surviving monster attacks the first surviving monster. All attacks in a round are applied simultaneously.

A monster dies when its health drops to 0 or below, and is removed from the battle at the end of the round. If only one monster (or none) remains, the battle ends early.

Return the list of health values of all surviving monsters after n rounds, in their original left-to-right order.

Function Signature

def monsters_battle(health: list[int], attack: list[int], n: int) -> list[int]:

Rules

  • Monster i starts with health health[i] and has attack power attack[i] .
  • At the start of a round, number the m survivors 0 to m - 1 in their original order. Survivor j deals damage equal to its attack power to survivor (j + 1) % m , so with two survivors each one hits the other.
  • Every survivor attacks in the round, including one that dies in that same round. All damage is subtracted first, and only then are monsters with health <= 0 removed.
  • If one or zero monsters remain at the start of a round, the battle stops. A lone monster never attacks itself.
  • Return the survivors' current health in original order. If every monster dies, return an empty list.

Constraints

  • 1 <= health.length <= 1000
  • health.length == attack.length
  • 1 <= health[i] <= 10000
  • 1 <= attack[i] <= 100
  • 1 <= n <= 10000

Examples

Input: health = [100,50,80], attack = [20,30,10], n = 3
Output: [70]

Each round, monster 0 hits monster 1 for 20, monster 1 hits monster 2 for 30, and monster 2 hits monster 0 for 10. Health goes [90,30,50], then [80,10,20], then [70,-10,-10], so monsters 1 and 2 die after round 3 and only 70 is left.

Input: health = [200,200,200,200], attack = [10,10,10,10], n = 2
Output: [180,180,180,180]

Every monster takes 10 damage per round from its left neighbor, giving [190,190,190,190] after round 1 and [180,180,180,180] after round 2. All survive.

Input: health = [10,30,30], attack = [5,5,10], n = 3
Output: [5,15]

Round 1 leaves [0,25,25], so monster 0 dies. From round 2 on, monsters 1 and 2 hit each other for 5 and 10 per round, so after two more rounds monster 1 has 25 - 20 = 5 and monster 2 has 25 - 10 = 15.