KatInterview

Turn-Based Squad Combat Engine with Deterministic Ordering

Data Structure DesignModel answer
MediumModel answerData Structure Designsimulationobject-oriented-designsorting

Last updated

Simulate a fight between two squads of monsters with fixed turn order, weakest-first targeting and a damage formula, and return the ordered list of attack events.

Squad Combat Engine

You are writing the battle core for a creature-fighting game. Every creature (a "monster") has these attributes: id, hp, attack, defense, speed, plus a multiplier that defaults to 1.0.

Two squads fight until one has nobody left. Implement simulate_battle(team_a, team_b) (you may back it with a BattleSimulator class) that returns the attack events in the order they happened.

class Monster:
    def __init__(self, id, hp, attack, defense, speed):
        self.id = id
        self.hp = hp
        self.attack = attack
        self.defense = defense
        self.speed = speed
        self.multiplier = 1.0

def simulate_battle(team_a, team_b):
    # returns a list of strings such as
    # "Monster 1 attacked Monster 4 for 10 damage"
    pass

Rules

  1. Acting order. A battle proceeds in rounds. Within a round, living monsters from both squads act one at a time, from highest speed to lowest. Equal speeds are broken by the smaller id going first.
  2. Choosing a target. The acting monster attacks the enemy with the lowest current hp. If several enemies share that value, it picks the one with the smallest id.
  3. Damage. damage = max(1, attacker.attack * attacker.multiplier - target.defense). The multiplier scales the attack value before the defense is subtracted. Damage must be an integer, so round the scaled attack down.
  4. Defeat. A monster whose hp reaches 0 or below leaves its squad immediately.
  5. End of battle. The fight stops as soon as one squad has no monsters left, even in the middle of a round.

Example

Squad A: {id: 1, hp: 50, atk: 20, def: 5, spd: 10} Squad B: {id: 2, hp: 30, atk: 10, def: 5, spd: 5}

Monster 1 is faster, so it attacks first and deals 20 - 5 = 15, leaving Monster 2 at 15 hp. The fight continues from there.

Constraints

  • Each squad holds 1 to 5 monsters.
  • hp > 0, and every stat is an integer from 1 to 100.
  • Ids are unique across both squads.

What to cover

Give your approach, explain any ambiguity you resolve and how, state the complexity, and provide working code. Also discuss how you would extend it to area-of-effect attacks and how you would make it fast enough to run thousands of battles.

Hints

Hint 1

Treat each round as a snapshot: sort all living monsters once by (-speed, id), then walk that list. What must you check before a monster in the snapshot actually acts?

Hint 2

Use a tuple key such as (current_hp, id) with min() for targeting, and keep hp in a separate map so the caller's Monster objects are not changed and re-runs are safe.

Hint 3

Check for an empty enemy squad right after every kill, not only at the end of a round. Also argue why the loop must terminate: every hit deals at least 1 damage and total hp is finite.