KatInterview

Duel Resolver: Speed, Types and Chip Damage

Data Structure DesignAuto-graded · 18 test cases
EasyAuto-gradedData Structure Designsimulationdictionariesfloating-point

Last updated

Simulate a two-creature duel with fixed speed order, a minimum-damage floor, and a type-advantage multiplier, then return the winner's name.

Duel Resolver

Two creatures fight in alternating attacks until one of them is knocked out. Given both creatures' stats, report who wins.

Each creature is a dictionary with these keys:

  • name (string)
  • hp (hit points)
  • atk (attack power)
  • def (defense)
  • spd (speed)
  • type (one of "Fire", "Water", "Grass")

Rules

Who strikes first. The creature with the larger spd attacks first. If the speeds are equal, the creature passed as the first argument attacks first. Speeds never change, so the attack order is the same for the whole duel: first mover, then second mover, then first mover again, and so on.

Damage of one attack.

  1. Compute base = max(1, (attacker_atk - defender_def) * 0.5). The floor of 1 is applied here, before any type bonus.
  2. If the attacker's type has the advantage over the defender's type, multiply base by 1.5. Otherwise multiply by 1.0.

Type advantages. Fire beats Grass, Grass beats Water, and Water beats Fire. Those are the only advantages. There is no penalty for a disadvantage, and two creatures of the same type get no bonus.

Applying damage. Keep HP as a floating-point number. After each attack, set the defender's HP to round(hp - damage, 2). Do not truncate fractional damage.

Ending the duel. Check for a knockout after every single attack. A creature with HP <= 0 is knocked out and never gets to attack again, even if it would have acted later in the same round.

  • If only the second creature is knocked out, return the first creature's name.
  • If only the first creature is knocked out, return the second creature's name.
  • If both are at <= 0 at the same moment, return "Draw".

Return the winner's name as a string, or "Draw".

Constraints

  • hp is greater than 0 for both creatures at the start.
  • atk, def and spd are non-negative numbers.
  • type is always one of "Fire", "Water", "Grass".
  • Every attack deals at least 1 damage, so the duel always ends.

Examples

Example 1

Input: run_duel({'name': 'A', 'hp': 20, 'atk': 10, 'def': 5, 'spd': 10, 'type': 'Fire'}, {'name': 'B', 'hp': 20, 'atk': 8, 'def': 4, 'spd': 8, 'type': 'Grass'})

Output: 'A'

Example 2

Input: run_duel({'name': 'Water', 'hp': 12, 'atk': 9, 'def': 2, 'spd': 5, 'type': 'Water'}, {'name': 'Fire', 'hp': 15, 'atk': 7, 'def': 3, 'spd': 5, 'type': 'Fire'})

Output: 'Water'

Example 3

Input: run_duel({'name': 'X', 'hp': 5, 'atk': 3, 'def': 1, 'spd': 4, 'type': 'Grass'}, {'name': 'Y', 'hp': 5, 'atk': 3, 'def': 1, 'spd': 4, 'type': 'Grass'})

Output: 'X'

Hints

Hint 1

The attack order depends only on speed, so decide it once before the loop instead of re-checking every turn.

Hint 2

Write a small helper that returns the damage of one attack. Apply the minimum of 1 first, then the 1.5 multiplier, because the order changes the result.

Hint 3

Check for a knockout after every attack, not once per round, so a creature that is already down never strikes back. Remember to round HP to 2 decimals after each hit.